IOS development-bottom layer-Class explanation, ios bottom layer-class explanation

Source: Internet
Author: User

IOS development-bottom layer-Class explanation, ios bottom layer-class explanation

Objective-c is iOS's development language. its true face is that it is not a real object-oriented language, and abstract understanding is just about it. In fact, it is C +. There is a formula that can be well interpreted.

OC = C + Runtime; Next let's talk about the objc-class in Runtime. Data preparation, objc4-646/runtime.

I. Class Definition

1.1 novel A objc-api.h in the OBJC_ISA_AVAILABILITY:

/* Introduce the role of _ attribute _ (deprecated). __attribute is a keyword that describes attributes for functions, variables, and classes. deprecated is incompatible with the original one.

If _ OBJC2 __, the compiler issues a warning for the original class */

#if !defined(OBJC_ISA_AVAILABILITY)#   if __OBJC2__#       define OBJC_ISA_AVAILABILITY  __attribute__((deprecated))#   else#       define OBJC_ISA_AVAILABILITY  /* still available */#   endif#endif

 

 

typedef struct objc_method *Method;typedef struct objc_ivar *Ivar;typedef struct objc_category *Category;typedef struct objc_property *objc_property_t;

 

 

1.2 next is the definition of Class in runtime. h:

Struct objc_class {Class isa OBJC_ISA_AVAILABILITY; // each Class has an isa pointer # if! _ OBJC2 _ Class super_class OBJC2_UNAVAILABLE; // parent Class const char * name ready; // Class name long version OBJC2_UNAVAILABLE; // Class version long info OBJC2_UNAVAILABLE ;//! *! Some bitwise identifiers used during runtime. For example, CLS_CLASS (0x1L) indicates that the class is a common class; CLS_META (0x2L) indicates that the class is metaclass (runtime. long instance_size OBJC2_UNAVAILABLE; // instance size struct objc_ivar_list * ivars memory; // memory address for storing each instance variable struct objc_method_list ** methodLists memory ;//! *! Determine whether it is a class or an instance based on info, and determine the function method to run, such as struct objc_cache * cache OBJC2_UNAVAILABLE; // cache struct objc_protocol_list * protocols OBJC2_UNAVAILABLE; // protocol # endif;

 

 

 

 

Ii. class initialization

One of the Runtime behaviors is initialize. During the program running, it calls initialize once for each class in your program. This call takes place before your class receives a message, but after its superclass receives initialize.

// There Is A objc-initialize.m file in Apple's official Runtime to interpret typedef struct _ objc_initializing_classes {int classesAllocated; // whether the Class is allocated with Class * metaclasses; // The parent Class of the Class, if there is no parent class, it is itself} _ objc_initializing_classes; // Initialize an object _ objc_initializing_classes // store all classes in the static linked list, use static _ objc_initializing_classes * _ fetchInitializingClassList (BOOL create) {_ objc_pthread_data * data; // list as the linked list _ objc_initializing_classes * list; Class * Classes; data = _ objc_fetch_pthread_data (create); if (data = nil) return nil; // Add the class node list to the linked list = data-> initializingClasses; if (list = nil) {if (! Create) {return nil;} else {list = (_ objc_initializing_classes *) _ calloc_internal (1, sizeof (_ objc_initializing_classes); data-> initializingClasses = list ;}} // connect the created classes to metaclasses and then classes = list-> metaclasses; if (classes = nil) {// If _ objc_initializing_classes exists, allocate metaclass array, // even if create = NO. // Allow 4 simultaneous class inits on this thread before realloc. list-> classesAllocated = 4; classes = (Class *) _ calloc_internal (list-> classesAllocated, sizeof (Class); list-> metaclasses = classes;} return list ;}

 

 

Iii. Operations of Class in runtime (important)

3.1 add * (add) 3.1.1 static IMP addMethod (Class cls, SEL name, IMP imp, const char * types, BOOL replace); // add method 3.1.2 BOOL class_addMethod (Class cls, SEL name, IMP imp, const char * types); // Add Class method 3.1.3 BOOL class_addIvar (Class cls, const char * name, size_t size, uint8_t alignment, const char * type); // Add the instance variable 3.1.4 static BOOL _ class_addProperty (Class cls, const char * name, const objc_property_attribute_t * attrs, unsigned int count, BOOL replace ); // Add attribute 3.2 replace * (modify) 3.2.1 IMP class_replaceMethod (Class cls, SEL name, IMP imp, const char * types); // modify method 3.2.2 void class_replaceProperty (Class cls, const char * name, const objc_property_attribute_t * attrs, unsigned int n); // modify attribute 3.3 get * (get) 3.3.1 static Class getClass (const char * name ); // obtain Class 3.3.2 static ivar_t * getIvar (Class cls, const char * name); // obtain Class variables (static equivalent to "+") 3.3.3 Method class_getInstanceMethod (Class cls, SEL sel); // obtain the instance Method 3.3.4 static Method _ class_getMethod (Class cls, SEL sel); // obtain the Class Method 3.3.5 static Protocol * getProtocol (const char * name ); // Add protocol 3.4 set * (set) 3.4.1 objc_class: setInitialized (); // set initialized initialization 3.4.2 static Class setSuperclass (Class cls, Class newSuper ); // set the parent class 3.5. Others are similar to void * objc_destructInstance (id obj). // destroy the Instance Object and so on.

 

 

Iv. Important Functions of Class

4.1 get * (get) 4.1.1 object_getClass (id obj); 4.1.2 IMP object_getMethodImplementation (id obj, SEL name); // obtain the instance method implementation 4.1.3 Ivar object_getInstanceVariable (id obj, const char * name, void ** value) // obtain instance attributes 4.2 set * (set) 4.2.1 Class object_setClass (id obj, Class cls); 4.2.2 Ivar object_setInstanceVariable (id obj, const char * name, void * value); // sets the instance attribute 4.2.3 void object_setIvar (id obj, Ivar ivar, id value) ; // Set instance variable 4.3 Other 4.3.1 static void _ class_resolveClassMethod (Class cls, SEL sel, id inst); // dynamically add Class methods, do not care whether the method 4.3.2 static void _ class_resolveInstanceMethod (Class cls, SEL sel, id inst) exists; // Add the implementation method dynamically, do not care whether the method 4.3.3 unsigned _ class_createInstancesFromZone (Class cls, size_t extraBytes, void * zone, id * results, unsigned num_requested) exists ); // create Instance Storage space 4.4 message forwarding 4.4.1 void instrumentObjcMessageSends (BOOL flag ); // Flag to pass in YES. All messages sent at run time will be printed to the/tmp/msgSend-xxxx file.

 

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.