The origin of the problem
Objective-c is a dynamic language, and whenever possible, objective-c always uses a dynamic approach to solve problems, such as when it is possible to defer compiling and connecting to the runtime, so it needs a powerful runtime system to execute compiled code. The role of runtime is similar to the objective-c language of the operating system, is the soul of objective-c. Many of the features of Objective-c are based on this powerful runtime and are well worth learning to understand.
Dynamic Language objective-c is a very common message sending statement (more accurately, the message is sent, in the case of no confusion I prefer to say method calls):
1 [receiver message];
At compile time, it translates into:
1 objc_msgsend (receiver, selector);
The subsequent work of compiling is then done.
There are, in fact, several similar approaches:
1 Objc_msgsend_stret (return value is struct) 2 Objc_msgsend_fpret (return value is floating point type) 3 Objc_msgsendsuper (Call parent class method) 4 Objc_msgsendsuper_stret (Call parent class method, return value is struct)
Compile-time just determined to send a specific message to the identified instance, and did not go to verify the implementation of the message (IMP), IDE Xcode does some basic validation, but the @selector () comes from the method, just a warning, there are often crashes.
The message implementation is not validated at compile time, and the message is sent only at run time.
Note: Personal understanding is that the implementation of a message is called a method, and the process of passing a method is called a message.
What is Selector/sel?
Selector/sel is also called the method selector, which is defined by the SEL in Objc.h, and "@selector ()" is the acquisition of a SEL pointer. Plainly, the method selector is simply a char * pointer, indicating that it represents the name of the method:
1 struct objc_selector *SEL; 2 // @selector is not a function call, just a hint to the compiler 3 NSLog (@ "%s", (char//Print Message
Objective-c at compile time, according to the name of the method, generate a unique ID to distinguish this method, this ID is the SEL type. It is important to note that as long as the names of the methods are the same, their IDs are the same, so objective-c, without overloading the concept , so you will see in objective-c a very strange way to name the phenomenon:
1 // -(void) SetWidth: (int) width; This function is rewritten as the following function 2 -(void) Setwidthintvalue: (int) width; 3 4 // -(void) SetWidth: (double) width; This function is rewritten as the following function 5 -(void) Setwidthdoublevalue: (double) width
To mention here, once you have the SEL, you can use Performselecor to send a message to an object:
1 [receiver Performselecor: @selector (message)];
However, it is generally recommended to use Respondstoselector to check whether an object can respond to this message.
Writing here, you may feel that this sel is similar to a function pointer in C/C + +, and you may be disappointed because IMP and function pointers are more like.
IMP is the abbreviation for "Implementation", which is the address of the Objetive-c method implementation code block, which can be called directly like a C function. Typically we send a message to an object through [object Method:parameter] or Objc_msgsend (), and then objective-c the runtime (objective-c Runtime) to find the IMP that matches this message. Then call it.
IMP is also a pointer to the address of the method, and the SEL is just an ID for the runtime to find the corresponding IMP from the method table. The SEL is the id,imp of the method and is the implementation of the method. Unlike function pointers in IMP and C + +, the value of a function pointer is determined at compile time, and IMP is determined at run time. The part about IMP is summed up after a few days to send up again, interested can also search first.
The selector is primarily used for loosely coupled communication between two objects . This method is used in many development environments, such as Gtk,delphi. Basically the entire cocoa library between objects, control communication between the building is on this basis. Selector is a very important part of objective-c dynamic implementation.
Selector/sel Storage, Lookup
Just said, the compiler generates a unique ID (SEL) at compile time based on the method name, which forms a collection that is maintained by the runtime (runtime). Runtime maintains these collections for all classes and instances in use, and in order to reduce the time cost of dynamic messaging, the runtime maintains a SEL cache for them. The method set and cache are implemented by the optimized hash, which can be used to speed up the query.
Obj in iOS inherits from NSObject, and there is a class Isa pointer in Nsobjcet.
1 @interface NSObject {2 Class Isa objc_isa_availability; 3 }
Look at class again:
1typedefstructObjc_class *Class;2 structObjc_class {3Class Isa;//point to Meta-class Metaclass4 5Class Super_class;//point to its parent class superclass6 Const Char*name;//class name7 Longversion;//class version information, initialization defaults to 0, can be modified by the runtime function class_setversion and class_getversion, read8 LongInfo//Some identifying information, such as Cls_class (0X1L), indicates that the class is normal class, which contains object methods and member variables; Cls_meta (0x2l) indicates that the class is a meta-class Metaclass, which contains class methods;9 LongInstance_size;//the instance variable size of the class (including instance variables inherited from the parent class);Ten structObjc_ivar_list *ivars;//the address used to store each member variable One structObjc_method_list **methodlists;//Some flags about info, such as Cls_class (0x1l), store the object method, such as Cls_meta (0X2L), and then store the class method; A structObjc_cache *cache;//a pointer to the nearest method used to improve efficiency; - structObjc_protocol_list *protocols;//Store The protocols that the class adheres to -}
As you can see, the ISA member of any class (inherited from NSObject) stores a lot of information about that class.
Class Isa: Point to Metaclass, which is the static class.
Class Super_class: Points to the parent class, or null if the class is a root class.
The following are the pointing relationships between Isa and superclass:
ISA pointer:
Isa in an obj instance points to the normal class, which stores ordinary member variables and object methods (the method that begins with "-").
The ISA pointer in the normal class points to the static class, which stores static type member variables and class methods (the "+" method at the beginning of the "+").
The ISA pointer in all meta-class metaclass points to the root meta-class root metaclass. The ISA pointer to root Metaclass also points to itself.
Super_class:
The normal class Super_class point to the normal class of its parent.
The super_class of the meta-class metaclass points to its parent class Metaclass.
Root meta-class root Metaclass Super_class point to the root class of ordinary classes, the root class of ordinary class Super_class is nil. (By the way, nil, nil, and Null are the same values in objective-c)
Let's take a look at how the specific message is sent after it's been dynamically searched for the corresponding method.
First, the compiler converts the code [receiver message] into Objc_msgsend (receiver, @selector (message)), in the Objc_msgsend function. First, the receiver corresponding class is found by the ISA pointer of receiver. In class, first go to the cache to find the corresponding function method through the SEL, if not found in the cache. Again to find in the methodlist, if not found in MethodList, then take superclass to find. If so, the method is added to the cache to facilitate the next lookup, and the found imp jumps to the corresponding function to execute.
Selector related uses
-(BOOL) iskindofclass:classobj//Determine if it is an instance of a class or its subclasses
-(BOOL) ismemberofclass:classobj//Determine if an instance of a class
-(BOOL) respondstoselector:selector//Determine if there is a method named after a name (encapsulated in a selector object)
+ (BOOL) instancesrespondtoselector:selector//Determine if the instance has a method named with a name. The difference between this and the above is that the previous method can be used on instances and classes, and this method can only be used on classes.
-(ID) performselector:selector//Send Message (call method)
.................. It's 1:30 in the day, it's all in the morning ......
....................................... T,t ........ ........................
iOS Learning-method Selector Selector related use