IOS Runtime notes and iosruntime notes

Source: Internet
Author: User

IOS Runtime notes and iosruntime notes

Objective-C language is a dynamic language that enables many static languages to process what they do during compilation and linking. The advantage of this dynamic language is that we are more flexible when writing code, for example, we can forward messages to the desired objects, or freely exchange the implementation of a method.

 

The Objective-C Class is represented by the Class type. It is actually a pointer to the objc_class struct. It is defined as follows:

typedef struct objc_class *Class;

View the definition of the objc_class struct in objc/runtime. h as follows:

Struct objc_class {Class isa OBJC_ISA_AVAILABILITY; # if! _ OBJC2 _ Class super_class OBJC2_UNAVAILABLE; // The parent Class const char * name OBJC2_UNAVAILABLE; // The Class name long version OBJC2_UNAVAILABLE; // The version information of the Class, the default value is 0 long info OBJC2_UNAVAILABLE; // class information, which indicates long instance_size OBJC2_UNAVAILABLE for runtime use; // The instance variable size of this class, struct objc_ivar_list * ivars OBJC2_UNAVAILABLE; // The member variable linked list of this class, struct objc_method_list ** methodLists OBJC2_UNAVAILABLE; // method-defined linked list, struct objc_cache * cache failed; // method-cached struct objc_protocol_list * protocols failed; // Protocol linked list # endif} OBJC2_UNAVAILABLE;

 

1,

Common examples

Class newClass = objc_allocateClassPair ([NSError class], "TestClass", 0); // create a new Class

Parameter 1: parent class
Parameter 2: subclass name
Parameter 3: extraBytes
Class_addMethod (newClass, @ selector (testMetaClass), (IMP) TestMetaClass, "v @:"); // Add a method for the new class
Parameter 1: Class Name
Parameter 2: Method Name
Parameter 3: method (IMP) Name of the method you write
Parameter 4: a string that defines the return value type and parameter type of the function dynamically determines the https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html Based on the return value and parameter here you can view the corresponding return value type expression v represents the following void: for example, if the parameter is defined as int :( id self, SEL _ cmd, NSString * name), it is like I.

Objc_registerClassPair (newClass); // register the created class id instance = [[newClass alloc] initWithDomain: @ "some domain" code: 0 userInfo: nil]; // instantiate the object [instance using mselector: @ selector (testMetaClass)]; // CALL THE METHOD

// Obtain the Class name const char * class_getName (Class cls );


// Obtain the parent Class class_getSuperclass (Class cls) of the Class; // judge whether the given Class is a metaclass (Class cls );

// Obtain the instance size

size_t class_getInstanceSize ( Class cls );


In objc_class, all member variables and attributes are stored in the ivars linked list. Ivars is
// Obtain the information of the specified instance member variable in the Class. Ivar class_getInstanceVariable (Class cls, const char * name); // obtain the information of the Class member variable. Ivar class_getClassVariable (Class cls, const char * name); // Add the member variable BOOL class_addIvar (Class cls, const char * name, size_t size, uint8_t alignment, const char * types ); // obtain the entire list of member variables Ivar * class_copyIvarList (Class cls, unsigned int * outCount );

The class_copyIvarList function returns an array of member variable information. Each element in the array is a pointer to the objc_ivar struct of the member variable information. This array does not contain the variables declared in the parent class. The outCount pointer returns the size of the array. Note that we must use free () to release this array.

// Obtain the specified attribute objc_property_t class_getProperty (Class cls, const char * name); // obtain the attribute list objc_property_t * class_copyPropertyList (Class cls, unsigned int * outCount ); // Add the BOOL class_addProperty (Class cls, const char * name, const objc_property_attribute_t * attributes, unsigned int attributeCount) for the Class attribute; // replace void class_replaceProperty (Class cls, const char * name, const objc_property_attribute_t * attributes, unsigned int attributeCount );


// Add Method BOOL class_addMethod (Class cls, SEL name, IMP imp, const char * types); // obtain the instance Method class_getInstanceMethod (Class cls, SEL name ); // obtain the Class Method class_getClassMethod (Class cls, SEL name); // obtain the Array Method * class_copyMethodList (Class cls, unsigned int * outCount) of all methods ); // The Implementation of the alternative method IMP class_replaceMethod (Class cls, SEL name, IMP imp, const char * types); // the specific implementation of the return method IMP class_getMethodImplementation (Class cls, SEL name); IMP class_getMethodImplementation_stret (Class cls, SEL name); // whether the Class instance responds to the specified selectorBOOL class_respondsToSelector (Class cls, SEL sel );

The implementation of class_addMethod overwrites the method implementation of the parent class, but does not replace the existing implementation in this class. If this class contains an implementation of the same name, the function returns NO. If you want to modify an existing implementation, you can use method_setImplementation. An Objective-C method is a simple C function that contains at least two parameters-self and _ cmd. Therefore, our implementation function (the IMP parameter points to the function) requires at least two parameters, as shown below:

void myMethodIMP(id self, SEL _cmd){    // implementation ....}


// Add the Protocol BOOL class_addProtocol (Class cls, protocol * Protocol); // whether the returned Class implements the specified protocol BOOL class_conformsToProtocol (Class cls, Protocol * protocol ); // return the Protocol list of Class implementation Protocol * class_copyProtocolList (Class cls, unsigned int * outCount );


// Obtain the int class_getVersion (Class cls); // set the void class_setVersion (Class cls, int version );

// Create a new Class and metadata Class objc_allocateClassPair (Class superclass, const char * name, size_t extraBytes); // destroy a Class and its associated Class void objc_disposeClassPair (Class cls ); // register void objc_registerClassPair (Class cls) created by objc_allocateClassPair in the application );

// Create a Class instance id class_createInstance (Class cls, size_t extraBytes); // create a Class instance id objc_constructInstance (Class cls, void * bytes) in the specified position ); // destroy class instance void * objc_destructInstance (id obj );


// Return a copy id object_copy (id obj, size_t size) of the specified object; // release the memory id object_dispose (id obj) occupied by the specified object );

// Modify the value of the instance variable of the class instance Ivar object_setInstanceVariable (id obj, const char * name, void * value); // obtain the value of the object instance variable Ivar object_getInstanceVariable (id obj, const char * name, void ** outValue); // returns the void * object_getIndexedIvars (id obj) pointer to any extra byte allocated to the given object ); // return the value id object_getIvar (id obj, Ivar ivar) of the instance variable in the object; // set the value void object_setIvar (id obj, Ivar ivar, id value) of the instance variable in the object)


// Return the Class name const char * object_getClassName (id obj) of the given object; // return the Class object_getClass (id obj) of the object ); // set the object Class object_setClass (id obj, Class cls );


// Obtain the list of registered Class definitions int objc_getClassList (Class * buffer, int bufferCount ); // create and return a list of pointers pointing to all registered Classes * objc_copyClassList (unsigned int * outCount); // return the Class definition Class objc_lookUpClass (const char * name) of the specified Class ); class objc_getClass (const char * name); Class objc_getRequiredClass (const char * name); // return the metadata Class objc_getMetaClass (const char * name) of the specified Class );
 

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.