标签:
Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理。这种动态语言的优势在于:我们写代码时更具灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一个方法的实现等。typedef struct objc_class *Class;查看objc/runtime.h中objc_class结构体的定义如下:
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;
在这个定义中,下面几个字段是我们感兴趣的
NSArray *array = [[NSArray alloc] init];
struct objc_object { Class isa OBJC_ISA_AVAILABILITY; }; typedef struct objc_object *id;可以看到,这个结构体只有一个字体,即指向其类的isa指针。这样,当我们向一个Objective-C对象发送消息时,运行时库会根据实例对象的isa指针找到这个实例对象所属的类。Runtime库会在类的方法列表及父类的方法列表中去寻找与消息对应的selector指向的方法。找到后即运行这个方法。
NSArray *array = [NSArray array];这个例子中,+array消息发送给了NSArray类,而这个NSArray也是一个对象。既然是对象,那么它也是一个objc_object指针,它包含一个指向其类的一个isa指针。那么这些就有一个问题了,这个isa指针指向什么呢?为了调用+array方法,这个类的isa指针必须指向一个包含这些类方法的一个objc_class结构体。这就引出了meta-class的概念:meta-class是一个类对象的类。
对于NSObject继承体系来说,其实例方法对体系中的所有实例、类和meta-class都是有效的;而类方法对于体系内的所有类和meta-class都是有效的。
讲了这么多,我们还是来写个例子吧:
void TestMetaClass(id self, SEL _cmd) { NSLog(@"This objcet is %p", self); NSLog(@"Class is %@, super class is %@", [self class], [self superclass]); Class currentClass = [self class]; for (int i = 0; i < 4; i++) { NSLog(@"Following the isa pointer %d times gives %p", i, currentClass); currentClass = objc_getClass((__bridge void *)currentClass); } NSLog(@"NSObject's class is %p", [NSObject class]); NSLog(@"NSObject's meta class is %p", objc_getClass((__bridge void *)[NSObject class])); } #pragma mark - @implementation Test - (void)ex_registerClassPair { Class newClass = objc_allocateClassPair([NSError class], "TestClass", 0); class_addMethod(newClass, @selector(testMetaClass), (IMP)TestMetaClass, "v@:"); objc_registerClassPair(newClass); id instance = [[newClass alloc] initWithDomain:@"some domain" code:0 userInfo:nil]; [instance performSelector:@selector(testMetaClass)]; } @end
这个例子是在运行时创建了一个NSError的子类TestClass,然后为这个子类添加一个方法testMetaClass,这个方法的实现是TestMetaClass函数。运行后,打印结果是
2014-10-20 22:57:07.352 mountain[1303:41490] This objcet is 0x7a6e22b0 2014-10-20 22:57:07.353 mountain[1303:41490] Class is TestStringClass, super class is NSError 2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 0 times gives 0x7a6e21b0 2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 1 times gives 0x0 2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 2 times gives 0x0 2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 3 times gives 0x0 2014-10-20 22:57:07.353 mountain[1303:41490] NSObject's class is 0xe10000 2014-10-20 22:57:07.354 mountain[1303:41490] NSObject's meta class is 0x0我们在for循环中,我们通过objc_getClass来获取对象的isa,并将其打印出来,依此一直回溯到NSObject的meta-class。分析打印结果,可以看到最后指针指向的地址是0x0,即NSObject的meta-class的类地址。
// 获取类的类名 const char * class_getName ( Class cls );● 对于class_getName函数,如果传入的cls为Nil,则返回一个字字符串。
// 获取类的父类 Class class_getSuperclass ( Class cls ); // 判断给定的Class是否是一个元类 BOOL class_isMetaClass ( Class cls );● class_getSuperclass函数,当cls为Nil或者cls为根类时,返回Nil。不过通常我们可以使用NSObject类的superclass方法来达到同样的目的。
// 获取实例大小 size_t class_getInstanceSize ( Class cls );
// 获取类中指定名称实例成员变量的信息 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 );● class_getInstanceVariable函数,它返回一个指向包含name指定的成员变量信息的objc_ivar结构体的指针(Ivar)。
// 获取指定的属性 objc_property_t class_getProperty ( Class cls, const char *name ); // 获取属性列表 objc_property_t * class_copyPropertyList ( Class cls, unsigned int *outCount ); // 为类添加属性 BOOL class_addProperty ( Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount ); // 替换类的属性 void class_replaceProperty ( Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount );
const uint8_t * class_getIvarLayout ( Class cls ); void class_setIvarLayout ( Class cls, const uint8_t *layout ); const uint8_t * class_getWeakIvarLayout ( Class cls ); void class_setWeakIvarLayout ( Class cls, const uint8_t *layout );但通常情况下,我们不需要去主动调用这些方法;在调用objc_registerClassPair时,会生成合理的布局。在此不详细介绍这些函数。
// 添加方法 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 ); // 类实例是否响应指定的selector BOOL class_respondsToSelector ( Class cls, SEL sel );● class_addMethod的实现会覆盖父类的方法实现,但不会取代本类中已存在的实现,如果本类中包含一个同名的实现,则函数会返回NO。如果要修改已存在实现,可以使用method_setImplementation。一个Objective-C方法是一个简单的C函数,它至少包含两个参数—self和_cmd。所以,我们的实现函数(IMP参数指向的函数)至少需要两个参数,如下所示:
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 );● class_conformsToProtocol函数可以使用NSObject类的conformsToProtocol:方法来替代。
// 获取版本号 int class_getVersion ( Class cls ); // 设置版本号 void class_setVersion ( Class cls, int version );
Class objc_getFutureClass ( const char *name ); void objc_setFutureClass ( Class cls, const char *name );通常我们不直接使用这两个函数。
//----------------------------------------------------------- // MyClass.h @interface MyClass : NSObject <NSCopying, NSCoding> @property (nonatomic, strong) NSArray *array; @property (nonatomic, copy) NSString *string; - (void)method1; - (void)method2; + (void)classMethod1; @end //----------------------------------------------------------- // MyClass.m #import "MyClass.h" @interface MyClass () { NSInteger _instance1; NSString * _instance2; } @property (nonatomic, assign) NSUInteger integer; - (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2; @end @implementation MyClass + (void)classMethod1 { } - (void)method1 { NSLog(@"call method method1"); } - (void)method2 { } - (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2 { NSLog(@"arg1 : %ld, arg2 : %@", arg1, arg2); } @end //----------------------------------------------------------- // main.h #import "MyClass.h" #import "MySubClass.h" #import <objc/runtime.h> int main(int argc, const char * argv[]) { @autoreleasepool { MyClass *myClass = [[MyClass alloc] init]; unsigned int outCount = 0; Class cls = myClass.class; // 类名 NSLog(@"class name: %s", class_getName(cls)); NSLog(@"=========================================================="); // 父类 NSLog(@"super class name: %s", class_getName(class_getSuperclass(cls))); NSLog(@"=========================================================="); // 是否是元类 NSLog(@"MyClass is %@ a meta-class", (class_isMetaClass(cls) ? @"" : @"not")); NSLog(@"=========================================================="); Class meta_class = objc_getMetaClass(class_getName(cls)); NSLog(@"%s's meta-class is %s", class_getName(cls), class_getName(meta_class)); NSLog(@"=========================================================="); // 变量实例大小 NSLog(@"instance size: %zu", class_getInstanceSize(cls)); NSLog(@"=========================================================="); // 成员变量 Ivar *ivars = class_copyIvarList(cls, &outCount); for (int i = 0; i < outCount; i++) { Ivar ivar = ivars[i]; NSLog(@"instance variable's name: %s at index: %d", ivar_getName(ivar), i); } free(ivars); Ivar string = class_getInstanceVariable(cls, "_string"); if (string != NULL) { NSLog(@"instace variable %s", ivar_getName(string)); } NSLog(@"=========================================================="); // 属性操作 objc_property_t * properties = class_copyPropertyList(cls, &outCount); for (int i = 0; i < outCount; i++) { objc_property_t property = properties[i]; NSLog(@"property's name: %s", property_getName(property)); } free(properties); objc_property_t array = class_getProperty(cls, "array"); if (array != NULL) { NSLog(@"property %s", property_getName(array)); } NSLog(@"=========================================================="); // 方法操作 Method *methods = class_copyMethodList(cls, &outCount); for (int i = 0; i < outCount; i++) { Method method = methods[i]; NSLog(@"method's signature: %s", method_getName(method)); } free(methods); Method method1 = class_getInstanceMethod(cls, @selector(method1)); if (method1 != NULL) { NSLog(@"method %s", method_getName(method1)); } Method classMethod = class_getClassMethod(cls, @selector(classMethod1)); if (classMethod != NULL) { NSLog(@"class method : %s", method_getName(classMethod)); } NSLog(@"MyClass is%@ responsd to selector: method3WithArg1:arg2:", class_respondsToSelector(cls, @selector(method3WithArg1:arg2:)) ? @"" : @" not"); IMP imp = class_getMethodImplementation(cls, @selector(method1)); imp(); NSLog(@"=========================================================="); // 协议 Protocol * __unsafe_unretained * protocols = class_copyProtocolList(cls, &outCount); Protocol * protocol; for (int i = 0; i < outCount; i++) { protocol = protocols[i]; NSLog(@"protocol name: %s", protocol_getName(protocol)); } NSLog(@"MyClass is%@ responsed to protocol %s", class_conformsToProtocol(cls, protocol) ? @"" : @" not", protocol_getName(protocol)); NSLog(@"=========================================================="); } return 0; }这段程序的输出如下:
2014-10-22 19:41:37.452 RuntimeTest[3189:156810] class name: MyClass 2014-10-22 19:41:37.453 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] super class name: NSObject 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] MyClass is not a meta-class 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.454 RuntimeTest[3189:156810] MyClass's meta-class is MyClass 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance size: 48 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _instance1 at index: 0 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _instance2 at index: 1 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _array at index: 2 2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _string at index: 3 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] instance variable's name: _integer at index: 4 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] instace variable _string 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] property's name: array 2014-10-22 19:41:37.463 RuntimeTest[3189:156810] property's name: string 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] property's name: integer 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] property array 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method1 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method2 2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method3WithArg1:arg2: 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: integer 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setInteger: 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: array 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: string 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setString: 2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setArray: 2014-10-22 19:41:37.466 RuntimeTest[3189:156810] method's signature: .cxx_destruct 2014-10-22 19:41:37.466 RuntimeTest[3189:156810] method method1 2014-10-22 19:41:37.466 RuntimeTest[3189:156810] class method : classMethod1 2014-10-22 19:41:37.466 RuntimeTest[3189:156810] MyClass is responsd to selector: method3WithArg1:arg2: 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] call method method1 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] ========================================================== 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] protocol name: NSCopying 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] protocol name: NSCoding 2014-10-22 19:41:37.467 RuntimeTest[3189:156810] MyClass is responsed to protocol NSCoding 2014-10-22 19:41:37.468 RuntimeTest[3189:156810] ==========================================================
// 创建一个新类和元类 Class objc_allocateClassPair ( Class superclass, const char *name, size_t extraBytes ); // 销毁一个类及其相关联的类 void objc_disposeClassPair ( Class cls ); // 在应用中注册由objc_allocateClassPair创建的类 void objc_registerClassPair ( Class cls );● objc_allocateClassPair函数:如果我们要创建一个根类,则superclass指定为Nil。extraBytes通常指定为0,该参数是分配给类和元类对象尾部的索引ivars的字节数。
Class cls = objc_allocateClassPair(MyClass.class, "MySubClass", 0); class_addMethod(cls, @selector(submethod1), (IMP)imp_submethod1, "v@:"); class_replaceMethod(cls, @selector(method1), (IMP)imp_submethod1, "v@:"); class_addIvar(cls, "_ivar1", sizeof(NSString *), log(sizeof(NSString *)), "i"); objc_property_attribute_t type = {"T", "@\"NSString\""}; objc_property_attribute_t ownership = { "C", "" }; objc_property_attribute_t backingivar = { "V", "_ivar1"}; objc_property_attribute_t attrs[] = {type, ownership, backingivar}; class_addProperty(cls, "property2", attrs, 3); objc_registerClassPair(cls); id instance = [[cls alloc] init]; [instance performSelector:@selector(submethod1)]; [instance performSelector:@selector(method1)];程序的输出如下:
2014-10-23 11:35:31.006 RuntimeTest[3800:66152] run sub method 1 2014-10-23 11:35:31.006 RuntimeTest[3800:66152] run sub method 1
// 创建类实例 id class_createInstance ( Class cls, size_t extraBytes ); // 在指定位置创建类实例 id objc_constructInstance ( Class cls, void *bytes ); // 销毁类实例 void * objc_destructInstance ( id obj );● class_createInstance函数:创建实例时,会在默认的内存区域为类分配内存。extraBytes参数表示分配的额外字节数。这些额外的字节可用于存储在类定义中所定义的实例变量之外的实例变量。该函数在ARC环境下无法使用。
id theObject = class_createInstance(NSString.class, sizeof(unsigned)); id str1 = [theObject init]; NSLog(@"%@", [str1 class]); id str2 = [[NSString alloc] initWithString:@"test"]; NSLog(@"%@", [str2 class]);输出结果是:
2014-10-23 12:46:50.781 RuntimeTest[4039:89088] NSString 2014-10-23 12:46:50.781 RuntimeTest[4039:89088] __NSCFConstantString可以看到,使用class_createInstance函数获取的是NSString实例,而不是类簇中的默认占位符类__NSCFConstantString。
// 返回指定对象的一份拷贝 id object_copy ( id obj, size_t size ); // 释放指定对象占用的内存 id object_dispose ( id obj );有这样一种场景,假设我们有类A和类B,且类B是类A的子类。类B通过添加一些额外的属性来扩展类A。现在我们创建了一个A类的实例对象,并希望在运行时将这个对象转换为B类的实例对象,这样可以添加数据到B类的属性中。这种情况下,我们没有办法直接转换,因为B类的实例会比A类的实例更大,没有足够的空间来放置对象。此时,我们就要以使用以上几个函数来处理这种情况,如下代码所示:
NSObject *a = [[NSObject alloc] init]; id newB = object_copy(a, class_getInstanceSize(MyClass.class)); object_setClass(newB, MyClass.class); object_dispose(a);2.针对对象实例变量进行操作的函数,这类函数包含:
// 修改类实例的实例变量的值 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 );如果实例变量的Ivar已经知道,那么调用object_getIvar会比object_getInstanceVariable函数快,相同情况下,object_setIvar也比object_setInstanceVariable快。
// 返回给定对象的类名 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 );● objc_getClassList函数:获取已注册的类定义的列表。我们不能假设从该函数中获取的类对象是继承自NSObject体系的,所以在这些类上调用方法是,都应该先检测一下这个方法是否在这个类中实现。
int numClasses; Class * classes = NULL; numClasses = objc_getClassList(NULL, 0); if (numClasses > 0) { classes = malloc(sizeof(Class) * numClasses); numClasses = objc_getClassList(classes, numClasses); NSLog(@"number of classes: %d", numClasses); for (int i = 0; i < numClasses; i++) { Class cls = classes[i]; NSLog(@"class name: %s", class_getName(cls)); } free(classes); }输出结果如下:
2014-10-23 16:20:52.589 RuntimeTest[8437:188589] number of classes: 1282 2014-10-23 16:20:52.589 RuntimeTest[8437:188589] class name: DDTokenRegexp 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: _NSMostCommonKoreanCharsKeySet 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: OS_xpc_dictionary 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: NSFileCoordinator 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: NSAssertionHandler 2014-10-23 16:20:52.590 RuntimeTest[8437:188589] class name: PFUbiquityTransactionLogMigrator 2014-10-23 16:20:52.591 RuntimeTest[8437:188589] class name: NSNotification 2014-10-23 16:20:52.591 RuntimeTest[8437:188589] class name: NSKeyValueNilSetEnumerator 2014-10-23 16:20:52.591 RuntimeTest[8437:188589] class name: OS_tcp_connection_tls_session 2014-10-23 16:20:52.591 RuntimeTest[8437:188589] class name: _PFRoutines ......还有大量输出● 获取类定义的方法有三个:objc_lookUpClass, objc_getClass和objc_getRequiredClass。如果类在运行时未注册,则objc_lookUpClass会返回nil,而objc_getClass会调用类处理回调,并再次确认类是否注册,如果确认未注册,再返回nil。而objc_getRequiredClass函数的操作与objc_getClass相同,只不过如果没有找到类,则会杀死进程。
标签:
原文地址:http://blog.csdn.net/shimazhuge/article/details/51429826