"Objective-c Runtime Analysis (ii)-CLASS,METHOD,SEL,IMP"

Source: Internet
Author: User

Main references in this series:
Objective-c Runtime Reference
Objective-c Runtime Programming Guide
Major documents involved: Objc/message.h,objc/objc-api.h,objc/objc.h,objc/runtime.h

Cool bar [tekuba.net] Use the Creative commons agreement "attribution-non-commercial use-consistent", using the content of this article please follow the agreement
OBJECTIVE-C Runtime is the basic content of objective-c, understanding OBJECTIVE-C Runtime is very useful for mastering the many technical principles of objective-c. Special cool bar specially organized the content of Objective-c runtime, a total of six articles, this article is the second article:
"Objective-c Runtime Analysis (i)-runtime preliminary"
"Objective-c Runtime Analysis (ii)-CLASS,METHOD,SEL,IMP"
"Objective-c Runtime Analysis (iii)-objc_msgsend"
"Objective-c Runtime Analysis (iv)--dynamic Method Resolution"
"Objective-c Runtime Analysis (v)-message Forwarding"
"Objective-c Runtime Analysis (vi)-type encodings & declared Properties"

This article refers to address: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ Ocrthowmessagingworks.html
This paper mainly analyzes several data types/concepts which are closely related to OBJECTIVE-C Runtime: Class, Method,,sel, IMP, they are all defined in objc/objc.h. Let's take a look at their definition.

FoldingExpandC + + Codecopy content to clipboard
  1. typedef   struct objc_class *class;
  2. typedef struct objc_object {
  3. Class Isa;
  4. } *id; // As you can see, an important ID in iOS is actually a pointer to Objc_object. The first object of NSObject is the class type of ISA. So the ID can mark all NSObject-based objects.   
  5. typedef   struct objc_selector *sel;
  6. #if! Objc_old_dispatch_prototypes
  7. typedef   void (*imp) (void / * ID, SEL, ... * / );
  8. #else   
  9. typedef  ID (*IMP) (ID, SEL, ...);
  10. #endif   


One, Class
Class is defined as a struct pointer to Objc_class that represents the class structure of a class. Objc_class is defined in Objc/objc_class.h as follows:

FoldingExpandC + + Codecopy content to clipboard
  1. struct Objc_class {
  2. Class Isa;
  3. #if!__objc2__   
  4. Class Super_class objc2_unavailable; / * Parent class * /   
  5. Const Char *name objc2_unavailable; / * Class name * /   
  6. Long version objc2_unavailable; / * Version information * /   
  7. Long info objc2_unavailable; / * Class information * /   
  8. Long instance_size objc2_unavailable; / * Instance size * /   
  9. struct objc_ivar_list *ivars objc2_unavailable; / * Instance parameter chain list * /   
  10. struct objc_method_list **methodlists objc2_unavailable; / * Class method Chain list * /   
  11. struct objc_cache *cache objc2_unavailable; / * Class method Cache * /   
  12. struct objc_protocol_list *protocols objc2_unavailable; / * Protocol Link list * /   
  13. #endif   
  14. } objc2_unavailable;


As you can see, 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 information.
The NSObject class method returns a pointer to its class structure. Each NSObject-based class instance object has 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.

Two, Method
method is defined in the runtime internally, defined in class with a objc_method_list, the list is Objc_method type , defined as follows:

FoldingExpandC + + Codecopy content to clipboard
  1. typedef   struct objc_method *method;
  2. struct Objc_method {
  3. SEL method_name objc2_unavailable; / * Flag Method Name * /   
  4. Char *method_types objc2_unavailable; / * Parameter type of method * /   
  5. IMP Method_imp objc2_unavailable; / * Function pointer pointing to the specific implementation of the method * /   
  6. }
  7. struct Objc_method_list {
  8. struct  objc_method_list *obsolete objc2_unavailable;
  9. int  Method_count objc2_unavailable;
  10. #ifdef __lp64__   
  11. int  space objc2_unavailable;
  12. #endif   
  13. / * Variable length structure * /   
  14. struct  Objc_method method_list[1] objc2_unavailable;
  15. }


Three, SEL
Defined as follows:
typedef struct OBJC_SELECTOR *sel;
Name/signature indicating the method
Example:
-(void) Hellotekuba: (nsstring *) URL port: (int) port
{
NSLog (@ "%@,%d", Url,port);
}

NSLog (@ "SEL =%s", @selector (Hellotekuba:port:));
Printing results:
SEL = Hellotekuba:port:
different classes can have the same selector, non-homogeneous instances of the object performselector the same selector, in their own method linked list according to selector to find a specific method to implement IMP, Then use this method to implement the specific implementation 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.

Four, IMP
typedef ID (*IMP) (ID, SEL, ...);
We know that the ID is a pointer to the Objc_object struct (see the definition of Objc_object earlier in this article), the struct has only one member Isa, so any class object that inherits from NSObject can be referred to by ID, because NSObject The first member instance is ISA.
The IMP is a function pointer that is directed to a function that contains an object ID for receiving the message, an optional SEL for the calling 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 parameter type and the return value type.

Five, other
Ivar
Runtime is used to denote instance variable, instance variables, associated with an object, cannot be used by static methods, and the corresponding is the class variable, which is declared as follows:
typedef struct OBJC_IVAR *ivar;

Category
The runtime is used to denote category, and its declaration is:
typedef struct OBJC_CATEGORY *category;
Catagory can dynamically add new behavior to a class that already exists. This ensures that the original design of the class is small and gradually expands as the function increases. When you use category to extend a class, you do not need to access its source code, and you do not need to create subclasses. For more catagory knowledge, refer to: http://www.tekuba.net/program/312/
Reprint please indicate from Cool bar, this article address: www.tekuba.net/program/336/
Recommended reading:
XCode Lipo Command use a bit of knowledge
An issue with Uinavigationcontroller eject (pop) and press-in (push) operations
An analysis of iOS background operation
IOS7 Background Fetch Background Application refresh
IOS Nsprocessinfo Get system start up time

Want to get updates on the cool bar in time? Want to know Ios,android develop the latest technology trends, click or scan the QR code below to download the "read More" App, rich ios,android,web and other fields developers blog with you to subscribe.

"Objective-c Runtime Analysis (ii)-CLASS,METHOD,SEL,IMP"

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.