01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Runtime type information<br />

171<br />

The if statement is designed to avoid the length method call when someString is<br />

nil, indicating no string is available:<br />

NSString *someString = ...;<br />

int numberOfCharacters = [someString length];<br />

If a message is sent to nil, or if a target object decides to completely ignore a message,<br />

the message in most cases will return the equivalent of zero. This means the code snippet<br />

just outlined would set numberOfCharacters to zero when you attempted to send<br />

the length message to the someString object.<br />

8.4 Runtime type information<br />

<strong>Objective</strong>-C provides rich type information at runtime about the objects and classes<br />

that live in your application. At its simplest, you can query an object to determine its<br />

type by sending it an isKindOfClass: message, as shown here:<br />

id someObject = ...;<br />

if ([someObject isKindOfClass:[CTRentalProperty class]]) {<br />

NSLog(@"This object is a kind of CTRentalProperty (or a subclass)");<br />

}<br />

The isKindOfClass: message returns true if the object is an instance of the specified<br />

class or is compatible with it (the object is of a subclass).<br />

8.4.1 Determining if a message will respond to a message<br />

If an object receives a message and determines it can’t process it, an error typically<br />

results. In some scenarios, especially with protocols with @optional messages, it can<br />

be helpful to determine if a particular message send will successfully execute or result<br />

in an error.<br />

Therefore, the NSObject class provides a message named respondsToSelector:<br />

that can be used to determine if an object will respond to (if it’ll handle or process) a<br />

given selector. You can use the respondsToSelector: message as follows:<br />

if ([anObject respondsToSelector:@selector(setAddress:)])<br />

[anObject setAddress:@"13 Adamson Crescent"];<br />

else<br />

NSLog(@"This object does not respond to the setAddress: selector");<br />

The respondsToSelector: test is especially important when sending messages to<br />

objects that you don’t have control over at compile time, particularly if the object is<br />

typed as id. For example, if you write code that sends a message to an object represented<br />

by a variable that others can set, you should make sure the receiver implements<br />

a method that can respond to the message.<br />

8.4.2 Sending a message generated at runtime<br />

Sometimes you need to send a message to an object but don’t know the name or set of<br />

arguments that must be included until runtime. NSObject comes to the rescue again by<br />

providing messages named performSelector:, performSelector:withObject:, and

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!