Discover Whether an Object Is an Instance of a Particular Class or its Subclasses
To discover whether an object is an instance of a class or its subclasses, call the
isKindOfClass:
method on the object. An app sometimes makes this check when it wants to discover the messages (implemented or inherited) that an app responds to.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, you call the class
method on the class symbol. Then 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 discover whether an object responds to a message, call the
respondsToSelector:
method on the object. App code often verifies that an object responds to a message before it sends the message to 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 discover whether an object conforms to a protocol, 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. Note than an object can conform to a protocol without implementing its optional methods.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 assume that the passed-in object is of the given type and raise an exception if it is not.Copy Objects
You make a copy of an object by sending a
copy
message to 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 specific to a class 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 (for example, the collection 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
.
No comments:
Post a Comment