Main references in this series:
Objective-c Runtime Reference
Objective-c Runtime Programming Guide
Major documents involved: Objc/message.h,objc/objc-api.h,objc/objc.h,objc/runtime.h
Cool bar [tekuba.net] Use the Creative commons agreement "attribution-non-commercial use-consistent", using the content of this article please follow the agreement
OBJECTIVE-C Runtime is the basic content of objective-c, understanding OBJECTIVE-C Runtime is very useful for mastering the many technical principles of objective-c. Special cool bar specially organized the content of Objective-c runtime, a total of six articles, this article is the third article:
"Objective-c Runtime Analysis (i)-runtime preliminary"
"Objective-c Runtime Analysis (ii)-CLASS,METHOD,SEL,IMP"
"Objective-c Runtime Analysis (iii)-objc_msgsend"
"Objective-c Runtime Analysis (iv)--dynamic Method Resolution"
"Objective-c Runtime Analysis (v)-message Forwarding"
"Objective-c Runtime Analysis (vi)-type encodings & declared Properties"
This section mainly refer to: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ Ocrthowmessagingworks.html#//apple_ref/doc/uid/tp40008048-ch104-sw1
Through the first two studies, I believe you have a little understanding of objective-c runtime, and we should learn the important "news" in Objective-c runtime.
One,obj_msgsend
Scene:
Tekuba *tekuba = [[Tekuba alloc] init];
[Tekuba URL];
When the [tekuba URL] is executed, the compiler automatically converts to Objc_msgsend (Tekuba, @selector (URL));
The prototype of Objc_msgsend is:
ID objc_msgsend (ID thereceiver, Seltheselector, ...)
The parameters are the message receiving object, the identifier of the method that corresponds to the message, and the parameters.
when executing the Objc_msgsend method, the following tasks are mainly accomplished:
The messaging function does everything necessary for dynamic binding:
(1), It first finds the procedure (method implementation), the selector refers to. Since the same method can be implemented differently by separate classes, the precise procedure that it finds depends on t He class of the receiver.
It first finds the SEL corresponding method to implement IMP. Because different classes may have different implementations for the same method, the method implementations found depend on the type of the message receiver.
(2), it then calls the procedure, passing it the receiving object (a pointer to its data), along with any arguments tha T were specified for the method.
The message Receiver object (a pointer to the message receiver object) and the parameters specified in the method are then passed to the method implementation IMP.
(3), Finally, it passes on the return value of the procedure as its own return value.
Finally, the return value of the method implementation is returned as the return value of the function.
note:the compiler generates calls to the messaging function. You should never call it directly in the code you write.
two, hidden parameters
When Objc_msgsend finds the procedure that implements a method, it calls the procedure and passes it all the arguments in The message. It also passes the procedure and the hidden arguments:
(1) The Receiving object
(2) The selector for the method
These arguments give every method implementation explicit information about the both halves of the message expression that Invoked it. They ' re said to being ' hidden ' because they aren ' t declared in the source code that defines the method. They ' re inserted into the implementation when the code is compiled.
When Objc_msgsend finds the implementation of the method, it calls the method implementation directly and passes all parameters in the message to the method implementation, and it passes two hidden parameters: (1) The recipient of the message, and (2) the method name SEL. These parameters help method implementations get information about the message expression. They are considered "hidden" because they are not declared in the source code of the definition method, but rather in the implementation of the Insert method when the code is compiled.
Although these parameters are not declared, they can still be referenced in the source code. The message receiver object can be referenced by self in the method, and the method itself is referenced by the _cmd. In the following example, _cmd refers to the strange method, and self refers to the object that receives the strange message.
FoldingExpandC + + Codecopy content to clipboard
- -Strange
- {
- ID target = Getthereceiver ();
- SEL method = Getthemethod ();
- if (target = = Self | | mothod = = _cmd)
- return Nil;
- return [Target Performselector:method];
- }
Self is more useful in these two parameters. In fact, it is the way to access instance variables of the message receiver object in the method implementation.
third, get the method address
the Methodforselector method in the NSObject class can get a pointer to the method implementation imp, and the pointer returned by METHODFORSELECTORL and the variable type of the assignment must be exactly the same, including the method's parameter type and the return value type.
The following example shows how to use pointers to invoke the setfilled: method implementation:
void (*setter) (ID, SEL, BOOL);
int i;
Setter = (void (*) (ID, SEL, BOOL)) [Target methodforselector: @selector (setfilled:)];
for (i = 0; i <; i++)
Setter (Targetlist[i], @selector (setfilled:), YES);
The first and arguments passed to the procedure is the receiving object (self) and the method selector (_cmd). These arguments was hidden in method syntax but must being made explicit when the method is called as a function.
Using methodforselector:to circumvent dynamic binding saves most of the time required by messaging. However, the savings would be significant only where a particular message is repeated many times, as in the For loop shown Above.
Note that Methodforselector:is provided by the Cocoa runtime system; It ' s not a feature of the objective-c language itself.
Using Methodforselector to avoid dynamic binding will reduce the overhead of most messages, but this only makes sense if the specified message is repeatedly sent many times, such as the For loop above. [The understanding of the cool bar is that in the NSObject-based object, there is a class attribute that maintains a method cache, and when multiple calls are made from the cache, the IMP is naturally faster].
Note that Methodforselector is the functionality provided by the Cocoa runtime system, not the functionality of the Objective-c language itself.
Reprint please indicate from Cool bar, this article address: www.tekuba.net/program/338/
Recommended reading:
IOS7 motion visual effects [Uiinterpolatingmotioneffect]
IOS coreanimation Core Animation (1)-calayer and caanimation Preliminary
Several schemes of TTS speech synthesis under iOS
IOS picture Tiling and stretching
IOS quartz2d Font stroke bold, etc.
Want to get updates on the cool bar in time? Want to know Ios,android develop the latest technology trends, click or scan the QR code below to download the "read More" App, rich ios,android,web and other fields developers blog with you to subscribe.
"Objective-c Runtime Analysis (iii)-objc_msgsend"