验证是否是类,对象是否对消息有回复,是否是接口,,比较两个对象是否相等,复制对象
Verify Object Capabilities at Runtime
Introspection, a powerful and useful feature of Objective-C and the NSObject
class, 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 the isKindOfClass:
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; |
} |
} |
The isKindOfClass:
method takes an object of type Class
as a parameter; to get this object, call the class
method on the class symbol. Evaluate the Boolean value returned by this method and proceed accordingly.
NSObject
declares 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 the respondsToSelector:
method on the object.
if ([item respondsToSelector:@selector(setState:)]) { |
[item setState:[self.arcView.font isBold] ? NSOnState : NSOffState]; |
} |
The 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 @selector
compiler 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 the conformsToProtocol:
method on the object.
- (void) setDelegate:(id __weak) obj { |
NSParameterAssert([obj conformsToProtocol: |
@protocol(SubviewTableViewControllerDataSourceProtocol)]); |
delegate = obj; |
} |
The conformsToProtocol:
method takes a runtime identifier of a protocol as a parameter; you specify this identifier using the @protocol
compiler directive. Evaluate the Boolean value returned by this method and proceed accordingly.
Compare Objects
You can compare two objects by using the isEqual:
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... |
} |
Note that object equality is different from object identity. For the latter, use the equality operator ==
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 isEqualTo
Type:
, 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 the copy
method on it:
NSArray *myArray = [yourArray copy]; |
To be copied, the class of the receiving object must conform to the NSCopying
protocol. If you want your objects to be copyable, you must adopt and implement the copy
method 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 mutableCopy
method to create a mutable copy of an object. For example, if you call mutableCopy
on an NSString
object, you get an instance of NSMutableString