验证是否是类,对象是否对消息有回复,是否是接口,,比较两个对象是否相等,复制对象
2012-04-26 11:07
716 查看
Verify Object Capabilities at Runtime
Introspection, a powerful and useful feature of Objective-C and theNSObjectclass, enables you to learn certain things about objects at runtime. You can thus avoid mistakes in your code such as sending a message to an object that doesn’t recognize it or assuming that an object inherits from a given class when it doesn’t.
There are three important types of information that an object can divulge about itself at runtime:
Whether it’s an instance of a particular class or subclass
Whether it responds to a message
Whether it conforms to a protocol
Discover Whether an Object Is an Instance of a Particular Class or its Subclasses
To do this, call theisKindOfClass:method on the object.
static int sum = 0; |
for (id item in myArray) { |
if ([item isKindOfClass:[NSNumber class]]) { |
int i = (int)[item intValue]; |
sum += i; |
} |
} |
isKindOfClass:method takes an object of type
Classas a parameter; to get this object, call the
classmethod on the class symbol. Evaluate the Boolean value returned by this method and proceed accordingly.
NSObjectdeclares other methods for discovering information about object inheritance. The
isMemberOfClass:method, for example, tells you whether an object is an instance of a specific class, whereas
isKindOfClass:tells you whether the object is a member of that class or any of its descendent classes.
Discover Whether an Object Responds to a Message
To do this, call therespondsToSelector:method on the object.
if ([item respondsToSelector:@selector(setState:)]) { |
[item setState:[self.arcView.font isBold] ? NSOnState : NSOffState]; |
} |
respondsToSelector:method takes a selector as its parameter. A selector is an Objective-C data type for runtime identifiers of methods; you specify a selector using the
@selectorcompiler directive. In your code, evaluate the Boolean value returned by this method and proceed accordingly.
For identifying the messages an object responds to, calling
respondsToSelector:is generally more useful than evaluating class type. For example, a more recent version of a class might implement a method that isn’t found in a prior version.
Discover Whether an Object Conforms to a Protocol
To do this, call theconformsToProtocol:method on the object.
- (void) setDelegate:(id __weak) obj { |
NSParameterAssert([obj conformsToProtocol: |
@protocol(SubviewTableViewControllerDataSourceProtocol)]); |
delegate = obj; |
} |
conformsToProtocol:method takes a runtime identifier of a protocol as a parameter; you specify this identifier using the
@protocolcompiler directive. Evaluate the Boolean value returned by this method and proceed accordingly.
Compare Objects
You can compare two objects by using theisEqual:method. The object receiving the message is compared to the passed-in object; if they’re the same, the method returns
YES. For example:
BOOL objectsAreEqual = [obj1 isEqual:obj2]; |
if (objectsAreEqual) { |
// do something... |
} |
==to test whether two variables point to the same instance.
What is compared when you compare two objects of the same class? That depends on the class. The root class,
NSObject, uses pointer equality as the basis of comparison. Subclasses at any level can override their superclass’s implementation to base the comparison on class-specific criteria, such as object state. For example, a hypothetical Person object might equal another Person object if the first-name, last-name, and birth-date attributes of both objects match.
The value and collection classes of the Foundation framework declare comparison methods of the form
isEqualToType
:, where Type is the class type minus the “NS” prefix—for example,
isEqualToString:and
isEqualToDictionary:. The comparison methods ensure the object passed in conforms to the given type before doing other kinds of comparisons.
Copy Objects
You make a copy of an object by calling thecopymethod on it:
NSArray *myArray = [yourArray copy]; |
NSCopyingprotocol. If you want your objects to be copyable, you must adopt and implement the
copymethod of this protocol.
You sometimes copy an object obtained from elsewhere in a program when you want to ensure the object’s state does not change while you’re using it.
Copying behavior is class-specific and depends upon the specific nature of the instance. Most classes implement deep copying, which makes a duplicate of all instance variables and properties; some classes implement shallow copying, which only duplicates the references to those instance variables and properties.
Classes that have mutable and immutable variants also declare a
mutableCopymethod to create a mutable copy of an object. For example, if you call
mutableCopyon an
NSStringobject, you get an instance of
NSMutableString
相关文章推荐
- Java 比较两个对象是否相等【含字符串,区分与不区分大小写】
- 深入java的equals方法,怎么比较两个对象的内容是否相等?对象的内容指的是什么?
- JS中如何比较两个Json对象是否相等实例代码
- 关于直接比较两个对象是否相等或则说List集合里是否包含某个对象的问题
- c\c++中比较两个字符串或string对象是否相等
- java 比较两个对象是否相等
- java比较两个对象是否相等的方法
- 直接比较两个对象是否相等返回false和List中判断是否包含某个对象的问题
- c\c++中比较两个字符串或string对象是否相等
- 比较两个Image对象是否相等、xib中设置自定义整个cell的高度
- c\c++中比较两个字符串或string对象是否相等
- java 反射比较两个类型相同的对象 内部的变量是否相等
- c\c++中比较两个字符串或string对象是否相等
- 比较两个对象的内容是否相等 重写object 的equals方法
- c\c++中比较两个字符串或string对象是否相等
- JS中如何比较两个Json对象是否相等
- 如何比较两个二维数组是否相等 - 回复 "阿拉丁" 的问题
- c\c++中比较两个字符串或string对象是否相等
- c\c++中比较两个字符串或string对象是否相等
- 比较两个Json对象是否相等