iOS underlying development messaging mechanism (i) Basic concepts

Source: Internet
Author: User

In the introductory-level OBJC tutorial, we often say to programmers who have turned from C + + or Java or other object-oriented languages that a method call in OBJC (the term in OBJC is a message) is similar to a method invocation in another language, except that the form is somewhat different.

For example, in C + +:

Bird * Abird = new Bird ();

Abird->fly ();

In OBJC, the following is true:

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

[Abird fly];

At first glance, it seems that the writing form is different, but the difference is big. Method invocations in C + + can be dynamic or static, while messages in OBJC are dynamic. The following is a detailed description of what is dynamic and what the compiler is doing behind it.

Class

1 structObjc_class {2 Class Isa objc_isa_availability;3 4 #if!__objc2__5 Class super_class objc2_unavailable;6     Const Char*name objc2_unavailable;7     Longversion objc2_unavailable;8     Longinfo objc2_unavailable;9     Longinstance_size objc2_unavailable;Ten     structObjc_ivar_list *Ivars objc2_unavailable; One     structObjc_method_list * *methodlists objc2_unavailable; A     structObjc_cache *cache objc2_unavailable; -     structObjc_protocol_list *protocols objc2_unavailable; - #endif the  - } objc2_unavailable; - /*Use ' Class ' instead of ' struct objc_class * '*/

This shows that class is a pointer to a class struct that contains a pointer to its parent class structure, a linked list of methods, the caching of such methods, 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 the corresponding parent class. One of the following:

As shown, the first instance variable of the instance object represented by the circle is Isa, which points to the class structure of the class the object ' s class. The class structure has a pointer to its parent class structure, superclass, and a method chain list of its own message name (selector)/implementation address.

Meaning of the method:

Note that the methods listed here are stored in the method list. The selector in Figure one refers to the SEL of method, the address means 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 that contains a method that sel– represents the name of the method, a types– represents the type of the method parameter, a IMP-pointer to a function that points to the specific implementation of the method.

What the SEL means:

In the preceding we see the definition of the method selection SEL as:

typedef struct OBJC_SELECTOR *sel;

It is a pointer to the Objc_selector that represents the name/signature of the method. Print out the selector as shown below.

-(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, which is not a problem, because instance objects of different classes performselector the same selector, in the respective message selection (selector)/Implementation Address method list according to Selector to find a specific method to implement IMP, and then use this method to implement the implementation of specific code. This is a dynamic binding process, at compile time, we do not know what will eventually execute some code, only when executing, through the selector to query, we can determine the specific execution code.

Meaning of IMP:

In the preceding we also see the definition of IMP as:

typedef ID (*IMP) (ID, SEL, ...);

Based on the definition of the previous ID, we know that the ID is a pointer to the Objc_object struct, which has only one member of ISA, so any class object that inherits from NSObject can be referred to by ID, because the first member instance of NSObject is Isa.

At this point, we know exactly what IMP means: IMP is a function pointer that contains an object ID (self pointer) that receives a message, an optional SEL (method name) that invokes the method, and an indefinite number of method parameters, and returns an ID. That is, the IMP is the execution code of the final invocation of the message and is the actual implementation code of the method. We can use this function pointer in the same way as in the C language.

Methodforselector in the NSObject class: The method is to get a pointer to the method implementation imp, Methodforselector: The returned pointer and the assigned variable type 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);

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.

Note that Methodforselector: is the functionality provided by the Cocoa runtime system, not the functionality of the Objective-c language itself.

iOS underlying development messaging mechanism (i) Basic concepts

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.