[IOS] in-depth introduction to ObjC messages

Source: Internet
Author: User

In-depth introduction to ObjC messages

Luo chaohui (Http://blog.csdn.net/kesalin)

Reprinted please indicate the source

In entry-level objc tutorials, we often say to programmers who transfer data from C ++, Java, or other object-oriented languages that method calls in objc (the term in objc is message) it is similar to method calls in other languages, but the form is somewhat different.

For example, in C ++:

Bird * abird = new bird ();

Abird-> fly ();

In objc:

Bird * abird = [[Bird alloc] init];

[Abird Fly];

At first glance, it seems that the writing format is different, but actually the difference is big. Method calls in C ++ may be dynamic or static, while messages in objc are dynamic. The following describes in detail why it is dynamic and what the compiler has done behind it.

To clarify the message topic, we must first understand three concepts: Class, Sel, and imp. They are defined in objc/objc. h:

Typedef struct objc_class * class;

Typedef struct objc_object {

Class ISA;

} * ID;

Typedef struct objc_selector * sel;

Typedef ID (* IMP) (ID, Sel ,...);

Meaning of Class

Class is defined as a struct pointer to objc_class, which represents the class structure of each class. The objc_class is defined in objc/objc_class.h as follows:

Struct objc_class {

Struct objc_classSuper_class ;/* Parent class */

Const char * Name;/* Class name */

Long version;/* Version information */

Long Info;/* Class information */

Long instance_size;/* Instance size */

Struct objc_ivar_list * ivars;/* Instance parameter linked list */

Struct objc_method_list ** methodlists;/* Method linked list */

Struct objc_cache * cache;/* Method cache */

Struct objc_protocol_list * protocols;/* Protocol linked list */

};

It can be seen that class is a pointer to a class struct, which contains a pointer to its parent class structure, a linked list of this class, cache of this class method and other necessary information.

The nsobject class method returns a pointer to its class structure. The first instance variable of each class instance object is a pointer to the class structure of the object, called Isa. With this pointer, the object can access its corresponding class and corresponding parent class. 1:

1. The first instance variable of the Instance Object represented by the circle is ISA, which points to the class structure of the object's class. This type of structure has a pointer superclass pointing to its parent class structure and a method linked list of its own message name (selector)/implementation address (address.

Meaning of the method:

Note that the method linked list stored here is of the method type. In Figure 1, selector refers to the sel of method, and address refers to the imp of method. Method is defined in the header file objc_class.h as follows:

Typedef struct objc_method * method;

Typedef struct objc _ method {

Sel method_name;

Char * method_types;

IMP method_imp;

};

A Method contains a Method named SEL-indicating the name of the Method, a types-representing the parameters of the Method, and an IMP-pointing to the specific implementation function pointer of the Method.

SEL meaning:

As we can see earlier, the definition of method selection SEL is:

Typedef struct objc_selector * SEL;

It is a pointer to objc_selector, indicating the method name. Print the selector as follows.

-(NSInteger) maxIn :( NSInteger) a theOther :( NSInteger) B

{

Return (a> B )? A: B;

}

NSLog (@ "SEL = % s", @ selector (maxIn: theOther :));

Output: SEL = maxIn: theOther:

Different classes can have the same selector. This is no problem, because when the instance objects of different classes have the same selector, they will be selected in their respective message selector) /implement the address method in the linked list to find the specific method to implement IMP, and then use this method to execute the specific implementation code. This is a process of dynamic binding. during compilation, we don't know which code will be executed in the end. We only query it through selector during execution, we can determine the specific Execution Code.

Meaning of IMP:

We can also see that IMP is defined:

Typedef id (* IMP) (id, SEL ,...);

According to the previous definition of ID, we know that ID is a pointer to the objc_object struct. This struct has only one ISA member, so any Class Object inherited from nsobject can be referred to by ID, because the first member instance of nsobject is ISA.

At this point, we can clearly understand the meaning of IMP: IMP is a function pointer, and the function to be directed contains an object ID (Self pointer) for receiving messages ), the optional sel (method name) of the call method, and the method parameters with an indefinite number, and an ID is returned. That is to say, IMP is the Execution Code of the final call of the message and the real implementation code of the method. We can use this function pointer like in C.

Methodforselector in the nsobject class: The method is used to obtain the pointer pointing to the method to implement imp. methodforselector: The returned pointer must be of the same type as the assigned variable, including the parameter type and Return Value Type of the method.

The following example shows how to use a pointer to call setfilled:

Void (* setter) (ID, Sel, bool );

Int I;

Setter = (void (*) (ID, Sel, bool) [target methodforselector: @ selector (setfilled :)];

For (I = 0; I <1000; I ++)

Setter (targetlist [I], @ selector (setfilled :), yes );

Using methodforselector: To avoid dynamic binding will reduce the overhead of most messages, but this is meaningful only when the specified message is repeatedly sent many times, such as the preceding for loop.

Note: methodforselector is a function provided by the cocoa runtime system, rather than the objective-C function.

Message calling process:

Now we have a general idea for the messages in ObjC: Example

Bird * aBird = [[Bird alloc] init];

[ABird fly];

For the fly call, the compiler inserts some code to convert it into calling the method to implement IMP, this IMP is found by finding the specific method corresponding to the selector with the fly name in the method linked list of the Bird class structure.

The above ideas have some unmentioned topics, such as the code inserted by the compiler. What if the corresponding IMP is not found in the method linked list? These topics will be discussed below.

Message function obj_msgSend:

The compiler converts a message to a call to the message function objc_msgSend. This function has two main parameters: the id of the message receiver and the method selection SEL of the message. At the same time, it receives any parameters in the message:

Id objc_msgSend (id theReceiver, SELtheSelector ,...)

The above message [aBird fly] will be converted to function calls in the following form:

Objc_msgSend (aBird, @ selector (fly ));

The message function performs all the work required for dynamic binding:
1. It first finds the method corresponding to SEL to implement IMP. Because different classes may have different implementations for the same method, the method implementation depends on the type of the message receiver.
2. Then, the Message Receiver object (pointer to the Message Receiver object) and the parameters specified in the method are passed to the method to implement IMP.
3. Finally, return the return value of the method implementation as the return value of the function.

The Editor automatically inserts the code that calls the message function objc_msgSend. You do not need to display the code that calls the message function. When objc_msgSend finds the implementation of the method, it will directly call this method implementation, and pass all the parameters in the message to the method implementation. At the same time, it will also pass two hidden parameters: the receiver of the message and the method name SEL. These parameter help methods obtain information about message expressions. They are considered to be "hidden" because they are not declared in the source code of the method definition, but are implemented during code compilation.

Although these parameters are not explicitly declared, they can still be referenced in the source code (just as the instance variables of the message recipient object can be referenced ). In the method, you can use self to reference the Message Receiver object and use the optional _ cmd to reference the method itself. In the following example, _ cmd refers to the strange method, and self refers to the object that receives the strange message.

-Strange

{

Id target = getTheReceiver ();

SEL method = getTheMethod ();

If (target = self | mothod = _ cmd)

Return nil;

Return [target parameter mselector: method];

}

Among the two parameters, self is more useful. In fact, it is a way to access the instance variable of the Message Receiver object in method implementation.

Procedure of searching IMP:

As mentioned above, objc_msgSend will find the method implementation imp in the method list of the class structure based on the method selection SEL. Here are some articles in the header. We also see a member named objc_cache * in the previous class structure. This cache exists to improve efficiency. Each class has an independent cache, including both the inherited methods and the methods defined in the class ..

When searching IMP:

1. First, search for the method cache of this class. If yes, return it;

2. If not found, go to the method list of the class. If it is found in the method list of this class, IMP is returned and added to the cache. According to the recent usage principle, this method is highly likely to be called again, and the cache can save the overhead of the next call and re-query. 3, 3. If the corresponding IMP is not found in the method list of this class, search for it in the method list of its parent class structure through the super_class pointer in this class structure, find the corresponding imp in the method list of a parent class, return it, and add it to the cache.

4. If the corresponding IMP is not found in the method list of itself and all parent classes, go to the Message forwarding process described below.

Convenience functions:

We can use some NSObject methods to obtain the runtime information or dynamically execute some messages:

Class returns the class of the object;

IsKindOfClass and isMemberOfClass check whether the object is in the specified class inheritance system;

RespondsToSelector checks whether the object can specify a message;

ConformsToProtocol checks whether the object implements the method of the specified Protocol Class;

MethodForSelector returns the address of the specified method implementation.

PerformSelector: withObject indicates the method used to execute SEL.

Message forwarding:

Normally, an error is prompted when a message cannot be processed by an object is sent. However, before the Objective-C runtime system throws an error, A special message forwardInvocation is sent to the message receiving object to notify the object. The unique parameter of the message is an NSInvocation object, which encapsulates the parameters of the original message and message.

We can implement forwardInvocation: The method to perform some default processing for messages that cannot be processed, or to forward messages to other objects for processing without throwing an error.

For the role of message forwarding, consider the following scenarios: Suppose we need to design an object that can respond to the negotiate message and include responses of other types of objects to the message. Through the implementation of the negotiate method, it is easy to forward the negotiate message to other objects.

Furthermore, let's assume that we want our object to be exactly the same as the response of another class object to the negotiate message. One possible way is to let our class inherit the implementation of methods of other classes. Then, sometimes this method is not feasible, because our class and other classes may need to respond to the negotiate message in different inheritance systems.

Although our class cannot inherit the negotiate method of other classes, we can still provide a method implementation, which simply forwards the negotiate message to other class objects, it's like "borrowing" from other classes. As follows:

-Negotiate

{

If ([someOtherObject respondsToSelector: @ selector (negotiate)])

Return [someOtherObject negotiate];

Return self;

}

This method is not flexible, especially when many messages want to be transmitted to other objects, we must provide methods for each message. In addition, this method cannot process unknown messages. When we write the code, all the sets of messages we need to forward must be determined. However, in fact, this set will change with the occurrence of running events, the definition of new methods or classes.

ForwardInvocation: a message provides a more special and dynamic solution to this problem: when an object cannot respond to a message because it does not have a corresponding implementation method, at runtime, the system will send a message to this object through forwardInvocation. Each object inherits the forwardInvocation: method from the NSObject class. However, the method implementation in NSObject simply calls doesNotRecognizeSelector :. By implementing our own forwardInvocation: method, we can forward messages to other objects in this method.

To forward messages to other objects, forwardInvocation: The methods must do the following:
1. decide who to forward the message to and
2. Forward the message with the original parameter.

Messages can be forwarded using the invokeWithTarget: method:

-(Void) forwardInvocation :( NSInvocation *) anInvocation

{

If ([someOtherObject respondsToSelector: [anInvocation selector])

[AnInvocation invokeWithTarget: someOtherObject];

Else

[Super forwardInvocation: anInvocation];

}

The returned value after the message is forwarded is returned to the original message sender. You can return any type of return values, including id, struct, and floating point.

ForwardInvocation: A method is like a distribution center for unrecognized messages and forwards these messages to different recipients. Or it can send all messages to the same receiving object as a transport station. It can translate a message into another message, or simply "eat" some messages, so there is no response or error. ForwardInvocation: The method can provide the same response to different messages, depending on the specific implementation of the method. This method provides the ability to link different objects to the message chain.

Note:ForwardInvocation: The method is called only when the message receiving object cannot respond to the Message normally. Therefore, if we want an object to forward the negotiate message to other objects, this object cannot have the negotiate method. No, forwardInvocation: it will not be called.

References:

Objective-CRuntime Reference:

Http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

Objective-C Runtime Programming Guide:
Http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Introduction/Introduction.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.