The accessing of associated objects is functionally similar to imaging that the object is an NSDictionary. An import difference is that key is treated purely as an opaque pointer. Thus, it's common to use static global variables for the keys.
Can be used on delegate protocol methods.
Can easily introduce hard-to-find bugs.
Item 11: Understand the Role of objc_msgSend
void objc_msgSend(id self, SEL cmd, ...)
The function loos through the list of methods implemented by the receiver's class. It caches the result in a fast map, one for each class.
Item 12: Understand Message Forwarding
It's not compile-time error to send a message to a class that it doesn't understand, since methods can be added to classes at runtime. When it receives a method that it doesn't understand, an object goes through message forwarding.
The forwarding pathways are split into two avenues: Dynamic Method Resolution +(BOOL)resolveInstanceMethod:(SEL)selector +(BOOL)resolveClassMethod:(SEL)selector Replacement Receiver -(id)forwardingTargetForSelector:(SEL)selector Full Forwarding Mechanism -(void)forwardInvocation:(NSInvocation*)invocaiton
Item 13: Consider Method Swizzling to Debug Opaque Methods
Swizzling is the process of swapping one method implementation for another, usually to add functionality to the original implementation.
沒有留言:
張貼留言