IOS Runtime Notes

Source: Internet
Author: User

Objective-c language is a dynamic language that puts a lot of static language in the process of compiling and linking time to the runtime. The advantage of this dynamic language is that we have more flexibility in writing code, such as the ability to forward messages to the object we want, or to exchange the implementation of a method at will.

The Objective-c class is represented by the class type, which is actually a pointer to the Objc_class struct body. It is defined as follows:

typedef struct objc_class *Class;

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

struct objc_class {    Class isa  OBJC_ISA_AVAILABILITY;#if !__OBJC2__    Class super_class                       OBJC2_UNAVAILABLE;  // 父类    const char *name                        OBJC2_UNAVAILABLE;  // 类名    long version                            OBJC2_UNAVAILABLE;  // 类的版本信息,默认为0    long info                               OBJC2_UNAVAILABLE;  // 类信息,供运行期使用的一些位标识    long instance_size                      OBJC2_UNAVAILABLE;  // 该类的实例变量大小    struct objc_ivar_list *ivars            OBJC2_UNAVAILABLE;  // 该类的成员变量链表    struct objc_method_list **methodLists   OBJC2_UNAVAILABLE;  // 方法定义的链表    struct objc_cache *cache                OBJC2_UNAVAILABLE;  // 方法缓存    struct objc_protocol_list *protocols    OBJC2_UNAVAILABLE;  // 协议链表#endif} OBJC2_UNAVAILABLE;

 

1.

Common examples

Class newClass = objc_allocateClassPair([NSError class], "TestClass", 0); // 创建新的类

参数1: 父类
参数2: 子类名
参数3: extraBytes
class_addMethod(newClass, @selector(testMetaClass), (IMP)TestMetaClass, "[email protected]:"); // 为新的类添加方法
参数1: 类名
参数2: 方法名称
参数3: 方法(IMP)你写的方法名
参数4: 一个定义该函数返回值类型和参数类型的字符串 根据返回值和参数动态的确定 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html 这里可以查看对应返回值类型的表达方法 v表示void 后边的:后边表示参数 比如说 定义 int:(id self, SEL _cmd,NSString * name) 那么就是 [email protected]:@ 这样的。

objc_registerClassPair(newClass); // 注册创建的类id instance = [[newClass alloc] initWithDomain:@"some domain" code:0 userInfo:nil]; // 实例化对象[instance performSelector:@selector(testMetaClass)]; // 调用方法

// 获取类的类名const char * class_getName ( Class cls );


// 获取类的父类Class class_getSuperclass ( Class cls );// 判断给定的Class是否是一个元类BOOL class_isMetaClass ( Class cls );

// 获取实例大小

size_t class_getInstanceSize ( Class cls );


在objc_class中,所有的成员变量、属性的信息是放在链表ivars中的。ivars是一个
// 获取类中指定名称实例成员变量的信息Ivar class_getInstanceVariable ( Class cls, const char *name );// 获取类成员变量的信息Ivar class_getClassVariable ( Class cls, const char *name );// 添加成员变量BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const char *types );// 获取整个成员变量列表Ivar * class_copyIvarList ( Class cls, unsigned int *outCount );

The Class_copyivarlist function, which returns an array of member variable information, each of which is a pointer to the OBJC_IVAR structure of the member variable information. This array does not contain variables declared in the parent class. The Outcount pointer returns the size of the array. It is important to note that we must use free () to release this array

 //Gets the specified property objc_property_t Class_getproperty (class cls, const char *name);//Get Property list objc_property_t * Class_co Pypropertylist (class cls, unsigned int *outcount);//Add property to class bool Class_addproperty (class CLS, const char *name, const objc_property_attribute_t *attributes, unsigned int attributecount);//Replace the property of the class void Class_replaceproperty (class cls, con St Char *name, const objc_property_attribute_t *attributes, unsigned int attributecount);  


// 添加方法BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types );// 获取实例方法Method class_getInstanceMethod ( Class cls, SEL name );// 获取类方法Method class_getClassMethod ( Class cls, SEL name );// 获取所有方法的数组Method * class_copyMethodList ( Class cls, unsigned int *outCount );// 替代方法的实现IMP class_replaceMethod ( Class cls, SEL name, IMP imp, const char *types );// 返回方法的具体实现IMP class_getMethodImplementation ( Class cls, SEL name );IMP class_getMethodImplementation_stret ( Class cls, SEL name );// 类实例是否响应指定的selectorBOOL class_respondsToSelector ( Class cls, SEL sel );

The implementation of Class_addmethod overrides the method implementation of the parent class, but does not supersede the implementation that already exists in this class, and if this class contains an implementation with the same name, the function returns No. If you want to modify an existing implementation, you can use Method_setimplementation. A Objective-c method is a simple C function that contains at least two parameters-self and _cmd. Therefore, our implementation function (the function that the IMP parameter points to) requires at least two parameters, as follows:

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


// 添加协议BOOL class_addProtocol ( Class cls, Protocol *protocol );// 返回类是否实现指定的协议BOOL class_conformsToProtocol ( Class cls, Protocol *protocol );// 返回类实现的协议列表Protocol * class_copyProtocolList ( Class cls, unsigned int *outCount );


// 获取版本号int class_getVersion ( Class cls );// 设置版本号void class_setVersion ( Class cls, int version );

// 创建一个新类和元类Class objc_allocateClassPair ( Class superclass, const char *name, size_t extraBytes );// 销毁一个类及其相关联的类void objc_disposeClassPair ( Class cls );// 在应用中注册由objc_allocateClassPair创建的类void objc_registerClassPair ( Class cls );

// 创建类实例id class_createInstance ( Class cls, size_t extraBytes );// 在指定位置创建类实例id objc_constructInstance ( Class cls, void *bytes );// 销毁类实例void * objc_destructInstance ( id obj );


// 返回指定对象的一份拷贝id object_copy ( id obj, size_t size );// 释放指定对象占用的内存id object_dispose ( id obj );

// 修改类实例的实例变量的值Ivar object_setInstanceVariable ( id obj, const char *name, void *value );// 获取对象实例变量的值Ivar object_getInstanceVariable ( id obj, const char *name, void **outValue );// 返回指向给定对象分配的任何额外字节的指针void * object_getIndexedIvars ( id obj );// 返回对象中实例变量的值id object_getIvar ( id obj, Ivar ivar );// 设置对象中实例变量的值void object_setIvar ( id obj, Ivar ivar, id value )


// 返回给定对象的类名const char * object_getClassName ( id obj );// 返回对象的类Class object_getClass ( id obj );// 设置对象的类Class object_setClass ( id obj, Class cls );


// 获取已注册的类定义的列表int objc_getClassList ( Class *buffer, int bufferCount );// 创建并返回一个指向所有已注册类的指针列表Class * objc_copyClassList ( unsigned int *outCount );// 返回指定类的类定义Class objc_lookUpClass ( const char *name );Class objc_getClass ( const char *name );Class objc_getRequiredClass ( const char *name );// 返回指定类的元类Class objc_getMetaClass ( const char *name );
 

IOS Runtime Notes

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.