Class type, Selector selector, SEL type, function pointer

Source: Internet
Author: User

first, class type

  • Mechanism: After a class is correctly compiled, in this properly compiled class, there is an Isa pointer to store the class's method and variable information, which is the ID type. You can get to this class type object through a normal string, or you can get to that type of object from this class, and after you get to the class type object, you can use it as if it were instantiated by the class. In the program runtime, objects that are instantiated by the class can be obtained from the class type object, and objects that cannot be determined at compile time can be obtained dynamically.

  • Because all the information about the class is stored in the class, you can get to the class type object by its name or by the object of the class. Of course, you can also get the class type object to the superclass.

    1 //A class message is sent to this class or object by means of its name or object , which gets the class type Object2Classclass= [Class name or Objectclass];3 //A superclass message is sent to this class or object through a class name or object to get the Super class type Object4Class superclass =[class name or object superclass];5 //gets the class-type object of this type through the class name string6Classclass=nsclassfromstring (class name string);7 //gets the name string for this class through the class type Object8NSString *classname =nsstringfromclass (Class parameter);9 //send a message to a class object, get the class name of the class, ClassName is a fixed parameter, detailed reference classname implement the source codeTenNSString *classname = [Classobject classname];

Second, Selector selector
    • Mechanism: The selector selector is used to represent the method, depending on the name of the method (parameter sequence), determine a unique ID (in fact, is an integer), (Don't touch * * in different classes within the same name of the same declaration method, its identity is the same). At runtime, you do not have to compare the name of the method to determine which method to perform, as long as you can navigate to the corresponding method with an integer, and then you can find the entry address of the corresponding method, and you can execute the method. in OC, the message mechanism is inefficient compared to the method of C language directly finding the function address entry method. Although OC uses the selector selection method to find, it always takes time. OC is compatible with the C language, that is, you can use the C function pointer method, increase efficiency. When efficiency is required, for example, when a function is used in a cycle, the way in which a message is sent to a single object is discarded (Selector), thus using the function pointer, which has the same efficiency as C.

Third, SEL type

  • In the selector selector has been said, at compile time, according to the method name and parameter sequence, determine a unique identification, this identity is an SEL type, can be obtained by means of the method name ID (Sel type Object), The SEL type object can also be obtained from a string of method names. Alternatively, the method name can be obtained from the SEL type Object (ID). As follows:
     1  //  2  SEL variable name = 3  //  4  SEL variable name = 5   Gets the method name  6  nsstring * variable name = 7   Through the SEL type object, gets the method name of this method  8  nsstring * Variable name = [SEL methodname]; 

  • Invocation mode
    1 // Object--receiver 2 [object performselector:sel type variable withobject: Parameter 1 withobject: Parameter 2 ...];

  • Benefit: Increase the flexibility of its method use, you can specify the method that the program executes by the configuration file (text file, XML file), read the configuration file at runtime, convert the method name or the method name string into the SEL type object, execute by sending the message. Because the method of finding in OC is by ID (an integer value), it is much more efficient to find a method than a matching string.
Iv. function pointers
  • format
     1  //  C function pointer, ID--receiver object, SEL--method Sel type Object, NSString *--method parameter (custom)  2  void  (*func) (id , SEL, NSString * 3   OC  4  IMP Funoc; 

    PS: Usage is the same as C.

  • Example
    1 ID cla = [[ClassE alloc] init]; 2 SEL Funoc = @Selector (fun); 3 void (*func) (ID, SEL, NSString *); 4 func = (ID, SEL, NSString *) [CLA Methodforselector:funoc]; 5 @" Test ");

    • Parameters (in OC, the first two parameters of a function pointer, i.e. ID and SEL are required, the remaining parameters are determined by the parameter sequence of the function pointer)
        1. ID--is an ID type that refers to the recipient object of the message. In runtime, the ID is actually the self object because you want to send a message from an object.
        2. Sel--is an SEL type, which refers to the ID of the method (method identification, an integer), in the process of sending the message through the SEL (method ID) of the _cmd method itself. That is, in the definition body of the method, the ID of the method itself can be obtained by _cmd.
        3. nsstring*---Custom parameters.

Class type, Selector selector, SEL type, function pointer

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.