First, understanding the SEL Preparation-----ISA pointer Brief
1. A class is like a C structure, NSObject declares a member variable: ISA. Because NSObject is the root class for all classes, all objects will have a member variable of ISA, which points to the class (space) of the object.
2, class in Objective-c is also an entity, because there are objective-c running environment of all classes will have their own storage space. Objective-c The running environment allocates space for each class. Isa, referred to here, is the space that points to such a class, thus establishing the correspondence between classes and objects.
3. Class space contains the member variables defined by the class, as well as the method implementation, and also contains pointers to their own parent class space.
4. There is also a pointer to Isa in class, pointing to Meteclass (Meta Class). The Meta class holds a list of class methods. When a class method is called, the implementation of the class method is looked up first from itself, and if not, the meta-class looks for the method to his parent class. Also note that the meta-Class (meteclass) is also a class, and it is also an object. the Meta class also has an Isa pointer, and its ISA pointer ultimately points to a root class (root meteclass). The ISA pointer to the root class points to itself, which forms a closed inner loop .
Ii. SEL and @selector ()
1. Selector, SEL, @selector
(1) The methods in the class are indexed by selector. the data type of the selector is SEL.
(2) Although the SEL is defined as char* ( string ), we can interpret it as an int. The name of each method corresponds to a unique int value. For example, method AddObject: Probably corresponds to 12, when looking for this method, using selector, instead of the name @ "AddObject:"
(3) in the OBJECTIVE-C data structure, there is a name-selector mapping table 3.16
(4) Sel This type essence is the number of the class method (function address, function pointer, string, int, function number)
(5) can understand @selector () is the number of the class method , his behavior is basically equivalent to the C language in the function pointer, except that the C language, can be directly assigned to a function pointer, and the OBJECT-C class cannot directly apply the function pointer, This can only be done with a @selector syntax to fetch. The result of the @selector is the SEL type, so plainly, @selector still get the number (address) of the method.
Small white Summary: SEL is actually a method of packaging, the method is packaged into an SEL type of data (selector), to find the corresponding method address, find the method address can call the method, these are run-time characteristics , send a message is sent selector, Then the address is found according to selector, and the method is called. If not found in the subclass, look up, until you find NSObject, no error. The SEL itself is a realistic use of runtime features!
2. Process and nature of method invocation
(1) Popular understanding-the process of searching for a method of object invocation (the process of finding a method): Selector selector mechanism
<1> first wrap the test method name into the SEL type data (a selector)
<2> find the corresponding method address based on SEL data
<3> method of invoking responses based on method address
<4> Note: There is a cache during this operation, the first time to look for a one to find (should be found in the code area, a line to find), consumption of performance after the use of the time to use directly (personal understanding: It should be placed in the "Class object" method list, which will not be repeated retrieval).
(2) Great God Understanding--objec_msgsend () function
<1> at compile time, as long as there are method calls, the compiler will look through selector, so (assuming AddObject's selector is a), call method :[MyObject Addobject:yourobject];
<2> above will be compiled into:
Objc_msgsend (MyObject, yourobject);
Here, the Objec_msgsend () function will use the MYOBJEC Isa pointer to find MyObject's class space structure and find the corresponding method for selector 12 in the class space structure. If not found, a pointer to the parent class will be used to locate the parent class space structure. Search for selector 12. If it is still not found, continue to find the parent class until it is found and will throw an exception if it is still not found in the root class NSObject.
<3> through the above, we can see that this is a very dynamic look-up process. The structure of the class can be changed at run time, which makes it easy to extend the function,objective-c language is a dynamic language and supports dynamic binding.
3. Examples of SEL applications:
The calling method (which itself wraps the method) can also define a variable of the SEL type, and then use the variable as a parameter
The use and principle of SEL in iOS