iOS development-The underlying article-class detailed

Source: Internet
Author: User

iOS development language Objective-c, its true face is that it is not a true object-oriented language, and abstract understanding of it. In fact, it is c+, there is a formula can be very good interpretation that is

OC = C + Runtime; Next we'll talk about the Objc-class under runtime. Prepare the information, Objc4-646/runtime.

One: class definition

1.1 Little explanation of the objc_isa_availability in objc-api.h:

/* To introduce the role of __attribute__ (deprecated), __attribute is a function, variable, class to do attribute description of the keyword, deprecated is to discard the original compatibility

If __objc2__, the original class, the compiler issued a warning */

#if !defined (objc_isa_availability)#   if  __objc2__#       define Objc_isa_ Availability  __attribute__ ((deprecated)) #   Else#       define Objc_isa_availability   /*  * *#   endif#endif

struct objc_method *struct objc_ivar *struct objc_category *  struct objc_property *objc_property_t;

1.2 Next is the definition of class in runtime.h:

structObjc_class {class Isa objc_isa_availability;//Each class has an Isa pointer    #if!__objc2__Class super_class objc2_unavailable;//Parent Class    Const Char*name objc2_unavailable;//class name    LongVersion objc2_unavailable;//class version    LongInfo objc2_unavailable;//!*! Some bit identifiers that are used for the run time. such as: Cls_class (0X1L) said that the class is the ordinary class; Cls_meta (0x2l) indicates that the class is metaclass and so on (Runtime.h is listed in detail)    LongInstance_size objc2_unavailable;//Instance Size    structObjc_ivar_list *ivars objc2_unavailable;//store memory addresses for each instance variable    structObjc_method_list **methodlists objc2_unavailable;//!*! determines whether a class or an instance is based on info information, what function method to run, etc.    structObjc_cache *cache objc2_unavailable;//Cache    structObjc_protocol_list *protocols objc2_unavailable;//Protocol#endif} objc2_unavailable;

Second: class initialization

One of the behaviors of runtime is initialize. During the run of the program, it invokes initialize in each class in your program. The time of this call occurs before your class receives the message, but after its superclass receives the initialize.

//There is an OBJC-INITIALIZE.M file in Apple's official runtime to readtypedefstruct_objc_initializing_classes {intclassesallocated;//class is assigned to existClass *metaclasses;//the parent class of the class, if there is no parent class that is itself} _objc_initializing_classes;//Initializes an object _objc_initializing_classes//store all classes in a static linked list for incoming and outgoing messagesStatic_objc_initializing_classes *_fetchinitializingclasslist (BOOL create) {_objc_pthread_data*data; //list for class linked list_objc_initializing_classes *list; Class*classes; Data=_objc_fetch_pthread_data (create); if(data = nil)returnNil; //link List Add Class nodeList = Data->initializingclasses; if(List = =Nil) {        if(!Create) {            returnNil; } Else{List= (_objc_initializing_classes *) _calloc_internal (1,sizeof(_objc_initializing_classes)); Data->initializingclasses =list; }    }    //classes The created is connected to metaclassesClasses = list->metaclasses; if(Classes = =Nil) {        //If _objc_initializing_classes exists, allocate metaclass array,//even if create = = NO. //Allow 4 simultaneous class inits on the this thread before realloc.list->classesallocated =4; Classes= (Class *) _calloc_internal (list->classesallocated,sizeof(Class)); List->metaclasses =classes; }    returnlist;}

Three. operation of class under runtime (important several)

3.1add*(Increase)3.1.1 StaticImp Addmethod (Class CLS, SEL name, IMP imp,Const Char*types, BOOL Replace);//Add Method        3.1.2BOOL Class_addmethod (class cls, SEL name, IMP imp,Const Char*types);//Add Class Method        3.1.3BOOL Class_addivar (class CLS,Const Char*name, size_t size,uint8_t alignment,Const Char*type);//Adding instance variables        3.1.4 StaticBOOL _class_addproperty (class CLS,Const Char*name,Constobjc_property_attribute_t *attrs, unsignedintCount,bool replace);//Add Property    3.2replace*(modified)3.2.1Imp Class_replacemethod (class CLS, SEL name, IMP imp,Const Char*types);//Modify Method        3.2.2 voidClass_replaceproperty (Class CLS,Const Char*name,Constobjc_property_attribute_t *attrs, unsignedintn);//Modifying Properties    3.3 Get*(GET)3.3.1 StaticClass GetClass (Const Char*name);//Get class        3.3.2 Staticivar_t *getivar (Class cls,Const Char*name);//Gets the class variable (static equals "+")        3.3.3Method Class_getinstancemethod (class CLS, Sel sel);//Get instance Method        3.3.4 StaticMethod _class_getmethod (class CLS, Sel sel);;//Get class Method        3.3.5 StaticProtocol *getprotocol (Const Char*name);//Add Agreement    3.4 Set*(SET)3.4.1Objc_class::setinitialized ();//initialized initialization of set        3.4.2 StaticClass Setsuperclass (class CLS, class Newsuper);//Set Parent class    3.5Others are similar tovoid*objc_destructinstance (IDOBJ);//destroying instance objects, etc.

Four. Important functions of class

4.1 Get*(GET)4.1.1Object_getclass (IDobj); 4.1.2IMP Object_getmethodimplementation (IDobj, SEL name);//obtaining an instance method implementation        4.1.3Ivar object_getinstancevariable (IDObjConst Char*name,void**value)//Get Instance Properties    4.2 Set*(SET)4.2.1Class Object_setclass (IDobj, Class cls); 4.2.2Ivar object_setinstancevariable (IDObjConst Char*name,void*value);//Setting Instance Properties        4.2.3 voidObject_setivar (IDobj, Ivar Ivar,IDValue);//Setting Instance variables    4.3other4.3.1 Static void_class_resolveclassmethod (class CLS, Sel sel,IDInst);//dynamically add class methods without worrying about whether the method exists        4.3.2 Static void_class_resolveinstancemethod (class CLS, Sel sel,IDInst);//dynamically add implementations without worrying about whether the method exists        4.3.3Unsigned _class_createinstancesfromzone (class cls, size_t extrabytes,void*zone,ID*results, unsigned num_requested);//Create an instance storage space    4.4Message Forwarding4.4.1 voidInstrumentobjcmessagesends (BOOL flag);//flag Incoming Yes, all messages sent at runtime are printed in the/tmp/msgsend-xxxx file. 

iOS development-The underlying article-class detailed

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.