13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

90CHAPTER 4: <strong>Objective</strong>-C Language Featuresid, too. Under ARC, this can cause serious problems (remember that the compiler needs todetermine whether to retain, release, or autorelease objects based on their usage), hence thecompiler error.But declaring the method is not enough. It is only enough to get the code to compile but notnecessarily run. At this point, calling doSomething on the string instance will generate a runtimeexception specifying that this string instance does not respond to the selector. When the runtimesearches the object hierarchy for the selector doSomething, it won’t find it.There are three message-forwarding steps that are taken when the runtime cannot locate aselector. If none of these three are successful, a runtime exception is generated.1. The runtime tries to resolve the selector at runtime and give the objecta chance to add an instance method to its Class object. For instancemethods, resolveInstanceMethod: is called on the Class object; for classmethods, resolveClassMethod: is called instead. Both of these methodshave a parameter—a SEL selector—that gets added to the Class.Note This lazy-loading approach isn’t strictly “forwarding” anything, but it is fast. Subsequentmessages with this selector will invoke the method (which was added on the first call).2. The runtime looks for an implementation of forwardingTargetForSelector:.This method returns a new object to which the message, unchanged, canbe forwarded. It can also return nilto skip to step 3 (helpful if you onlywant to forward some of the messages). Returning self might force thecode into an infinite loop, so be careful!3. The runtime looks for an implementation of bothmethodSignatureForSelector: and forwardInvocation: in your instance.These are methods for (possibly) modifying the original message andforwarding it onto another object.The process takes two steps. First, methodSignatureForSelector: is called to obtainan NSMethodSignature object that defines the return type and parameter list of theselector. Subsequently, the runtime calls forwardInvocation: with an NSInvocationobject that wraps the NSMethodSignature. The NSInvocation object can be invoked bycalling invokeWithTarget: with the object to which you want to forward the messageas a parameter.If this final step fails to properly handle the message, the runtime will senddoesNotRecognizeSelector: and a runtime exception is raised.Naturally, implementing any or all of these steps is possible and different actions can be takendepending on the selector in question.This is quite a complex topic; Listing 4-21 provides a complete (though somewhat pedagogical)example of using all three methods.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!