博客> Objc Runtime
Objc Runtime
2017-12-13 14:55 评论:0 阅读:363 诸葛亮倒骑小毛驴
objc RuntimeIOS

概述

Objc Runtime使得C具有了面向对象能力,在程序运行时创建,检查,修改类、对象和它们的方法。 Runtime是C和汇编编写的,这里http://www.opensource.apple.com/source/objc4/可以下到苹果 维护的开源代码,GNU也有一个开源的runtime版本,他们都努力的保持一致。 苹果官方的Runtime编程指南

Runtime函数

Runtime系统是由一系列的函数和数据结构组成的公共接口动态共享库, 在/usr/include/objc目录下可以看到头文件,可以用其中一些函数通过C语 言实现Objective-C中一样的功能。 苹果官方文档 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html 里有详细的Runtime函数文档。

Class和Object基础数据结构

Class

objc/runtime.h中objc_class结构体的定义如下:

struct objc_class { Class isa OBJC_ISA_AVAILABILITY; //isa指针指向Meta Class,因为Objc的类的本身也是一个Object,为了处理这个关系,runtime就创造了Meta Class,当给类发送[NSObject alloc]这样消息时,实际上是把这个消息发给了Class Object

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; // 方法缓存,对象接到一个消息会根据isa指针查找消息对象,这时会在methodLists中遍历,如果cache了,常用的方法调用时就能够提高调用的效率。 struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; // 协议链表

endif

} OBJC2_UNAVAILABLE;

objc_ivar_list和objc_method_list的定义

//objc_ivar_list结构体存储objc_ivar数组列表 struct objc_ivar_list { int ivar_count OBJC2_UNAVAILABLE;

ifdef LP64

int space OBJC2_UNAVAILABLE;

endif

/* variable length structure */
struct objc_ivar ivar_list[1] OBJC2_UNAVAILABLE;

} OBJC2_UNAVAILABLE;

//objc_method_list结构体存储着objc_method的数组列表 struct objc_method_list { struct objc_method_list *obsolete OBJC2_UNAVAILABLE; int method_count OBJC2_UNAVAILABLE;

ifdef LP64

int space OBJC2_UNAVAILABLE;

endif

/* variable length structure */
struct objc_method method_list[1] OBJC2_UNAVAILABLE;

}

objc_object与id

objc_object是一个类的实例结构体,objc/objc.h中objc_object是一个类的实例结构体定义如下:

struct objc_object { Class isa OBJC_ISA_AVAILABILITY; };

typedef struct objc_object *id;

向object发送消息时,Runtime库会根据object的isa指针找到这个实例object所属于的类,然后在类的方法列表以及父类方法列表寻找对应的方法运行。id是一个objc_object结构类型的指针,这个类型的对象能够转换成任何一种对象。

objc_cache

objc_class结构体中的cache字段用于缓存调用过的method。cache指针指向objc_cache结构体,这个结构体的定义如下

struct objc_cache { unsigned int mask / total = mask + 1 / OBJC2_UNAVAILABLE; //指定分配缓存bucket的总数。runtime使用这个字段确定线性查找数组的索引位置 unsigned int occupied OBJC2_UNAVAILABLE; //实际占用缓存bucket总数 Method buckets[1] OBJC2_UNAVAILABLE; //指向Method数据结构指针的数组,这个数组的总数不能超过mask+1,但是指针是可能为空的,这就表示缓存bucket没有被占用,数组会随着时间增长。 };

Meta Class

meta class是一个类对象的类,当向对象发消息,runtime会在这个对象所属类方法列表中查找发送消息对应的方法,但当向类发送消息时,runtime就会在这个类的meta class方法列表里查找。所有的meta class,包括Root class,Superclass,Subclass的isa都指向Root 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 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>superclass)
      if (cls == (Class)aClass)
          return YES;
return NO;

}

  • (BOOL)isMemberOf:aClass { return isa == (Class)aClass; }

res1中,可以从isKindOfClass看出NSObject class的isa第一次会指向NSObject的Meta Class,接着Super class时会NSObject的Meta Class根据前面讲的闭环可以知道是会指到NSObject class,这样res1的bool值就是真了。

res2的话因为是isMemberOf就只有一次,那么是NSObject的Meta Class和NSObject class不同返回的bool值就是false了。

res3第一次是Sark Meta Class,第二次super class 后就是NSObject Meta Class了,返回也是false

res4是Sark Meta Class,所以返回也是false

类与对象操作函数

runtime有很多的函数可以操作类和对象。类相关的是class为前缀,对象相关操作是objc或object_为前缀。

类相关操作函数

name

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

super_class和meta-class

// 获取类的父类 Class class_getSuperclass ( Class cls );

// 判断给定的Class是否是一个meta class BOOL class_isMetaClass ( Class cls );

instance_size

// 获取实例大小 size_t class_getInstanceSize ( Class cls );

成员变量(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 ); //这个只能够向在runtime时创建的类添加成员变量

// 获取整个成员变量列表 Ivar class_copyIvarList ( Class cls, unsigned int outCount ); //必须使用free()来释放这个数组

//属性操作函数 // 获取类中指定名称实例成员变量的信息 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 );

methodLists

// 添加方法 BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types ); //和成员变量不同的是可以为类动态添加方法。如果有同名会返回NO,修改的话需要使用method_setImplementation

// 获取实例方法 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 );

objc_protocol_list

// 添加协议 BOOL class_addProtocol ( Class cls, Protocol *protocol );

// 返回类是否实现指定的协议 BOOL class_conformsToProtocol ( Class cls, Protocol *protocol );

// 返回类实现的协议列表 Protocol class_copyProtocolList ( Class cls, unsigned int outCount );

version

// 获取版本号 int class_getVersion ( Class cls );

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

实例

通过实例来消化下上面的那些函数

//----------------------------------------------------------- // MyClass.h @interface MyClass : NSObject @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

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 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed> 0) {
classes = malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
NSLog(@"number of classes: %d", numClasses);

for (int i = 0; i < numClasses xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>

@implementation UIViewController (Tracking)

  • (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; // When swizzling a class method, use the following: // Class class = object_getClass((id)self);

      //通过method swizzling修改了UIViewController的@selector(viewWillAppear:)的指针使其指向了自定义的xxx_viewWillAppear
      SEL originalSelector = @selector(viewWillAppear:);
      SEL swizzledSelector = @selector(xxx_viewWillAppear:);
    
      Method originalMethod = class_getInstanceMethod(class, originalSelector);
      Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
      BOOL didAddMethod = class_addMethod(class,
          originalSelector,
          method_getImplementation(swizzledMethod),
          method_getTypeEncoding(swizzledMethod));
    
      //如果类中不存在要替换的方法,就先用class_addMethod和class_replaceMethod函数添加和替换两个方法实现。但如果已经有了要替换的方法,就调用method_exchangeImplementations函数交换两个方法的Implementation。
      if (didAddMethod) {
          class_replaceMethod(class,
                swizzledSelector,
                method_getImplementation(originalMethod),
          method_getTypeEncoding(originalMethod));
      } else {
          method_exchangeImplementations(originalMethod, swizzledMethod);
      }

    }); }

pragma mark - Method Swizzling

  • (void)xxx_viewWillAppear:(BOOL)animated { [self xxx_viewWillAppear:animated]; NSLog(@"viewWillAppear: %@", self); }

@end

method_exchangeImplementations做的事情和如下代码是一样的

IMP imp1 = method_getImplementation(m1); IMP imp2 = method_getImplementation(m2); method_setImplementation(m1, imp2); method_setImplementation(m2, imp1);

另一种Method Swizzling的实现

  • (void)replacementReceiveMessage:(const struct BInstantMessage *)arg1 { NSLog(@"arg1 is %@", arg1); [self replacementReceiveMessage:arg1]; }
  • (void)load { SEL originalSelector = @selector(ReceiveMessage:); SEL overrideSelector = @selector(replacementReceiveMessage:); Method originalMethod = class_getInstanceMethod(self, originalSelector); Method overrideMethod = class_getInstanceMethod(self, overrideSelector); if (class_addMethod(self, originalSelector, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) { class_replaceMethod(self, overrideSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, overrideMethod); } }

这里有几个关于Method Swizzling的资源可以参考

Protocol和Category

基础数据类型

Category

指向分类的结构体的指针

typedef struct objc_category *Category;

struct objc_category { char category_name OBJC2_UNAVAILABLE; // 分类名 char class_name OBJC2_UNAVAILABLE; // 分类所属的类名 struct objc_method_list instance_methods OBJC2_UNAVAILABLE; // 实例方法列表 struct objc_method_list class_methods OBJC2_UNAVAILABLE; // 类方法列表,Meta Class方法列表的子集 struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; // 分类所实现的协议列表 }

Category里面的方法加载过程,objc源码中找到objc-os.mm,函数_objc_init就是runtime的加载入口由libSystem调用,开始初始化,之后objc-runtime-new.mm里的map_images会加载map到内存,_read_images开始初始化这个map,这时会load所有Class,Protocol和Category,NSObject的+load方法就是这个时候调用的。下面是加载代码

// Discover categories. for (EACH_HEADER) { category_t *catlist = _getObjc2CategoryList(hi, &count); for (i = 0; i < count xss=removed xss=removed>cls); if (!cls) { // Category's target class is missing (probably weak-linked). // Disavow any knowledge of this category. catlist[i] = nil; if (PrintConnecting) { _objc_inform("CLASS: IGNORING category \?\?\?(%s) %p with " "missing weak-linked target class", cat->name, cat); } continue; } // Process this category. // First, register the category with its target class. // Then, rebuild the class's method lists (etc) if // the class is realized. BOOL classExists = NO; if (cat->instanceMethods || cat->protocols || cat->instanceProperties) { addUnattachedCategoryForClass(cat, cls, hi); if (cls->isRealized()) { remethodizeClass(cls); classExists = YES; } if (PrintConnecting) { _objc_inform("CLASS: found category -%s(%s) %s", cls->nameForLogging(), cat->name, classExists ? "on existing class" : ""); } } if (cat->classMethods || cat->protocols / || cat->classProperties */) { addUnattachedCategoryForClass(cat, cls->ISA(), hi); if (cls->ISA()->isRealized()) { remethodizeClass(cls->ISA()); } if (PrintConnecting) { _objc_inform("CLASS: found category +%s(%s)", cls->nameForLogging(), cat->name); } } } }

//调用remethodizeClass方法,在其实现里调用attachCategoryMethods static void attachCategoryMethods(Class cls, category_list cats, bool flushCaches) { if (!cats) return; if (PrintReplacedMethods) printReplacements(cls, cats); bool isMeta = cls->isMetaClass(); method_list_t mlists = (method_list_t ) _malloc_internal(cats->count sizeof(mlists)); // Count backwards through cats to get newest categories first int mcount = 0; int i = cats->count; BOOL fromBundle = NO; while (i--) { method_list_t mlist = cat_method_list(cats->list[i].cat, isMeta); if (mlist) { mlists[mcount++] = mlist; fromBundle |= cats->list[i].fromBundle; } } attachMethodLists(cls, mlists, mcount, NO, fromBundle, flushCaches); _free_internal(mlists); }

示例,下面的代码会编译错误,Runtime Crash还是会正常输出

@interface NSObject (Sark)

  • (void)foo; @end @implementation NSObject (Sark)
  • (void)foo { NSLog(@"IMP: -[NSObject(Sark) foo]"); } @end int main(int argc, const char * argv[]) { @autoreleasepool { [NSObject foo]; [[NSObject new] foo]; } return 0; } //结果,正常输出结果如下 2014-11-06 13:11:46.694 Test[14872:1110786] IMP: -[NSObject(Sark) foo] 2014-11-06 13:11:46.695 Test[14872:1110786] IMP: -[NSObject(Sark) foo]

objc runtime加载后NSObject的Sark Category被加载,头文件+(void)foo没有IMP,只会出现一个warning。被加到Class的Method list里的方法只有-(void)foo,Meta Class的方法列表里没有。

执行[NSObject foo]时,会在Meta Class的Method list里找,找不着就继续往super class里找,NSObject Meta Clas的super class是NSObject本身,这时在NSObject的Method list里就有foo这个方法了,能够正常输出。

执行[[NSObject new] foo]就简单的多了,[NSObject new]生成一个实例,实例的Method list是有foo方法的,于是正常输出。

Protocol

Protocol其实就是一个对象结构体

typedef struct objc_object Protocol;

操作函数

Category操作函数信息都包含在objc_class中,我们可以通过objc_class的操作函数来获取分类的操作函数信息。

@interface RuntimeCategoryClass : NSObject

  • (void)method1; @end

@interface RuntimeCategoryClass (Category)

  • (void)method2; @end

@implementation RuntimeCategoryClass

  • (void)method1 { }

@end

@implementation RuntimeCategoryClass (Category)

  • (void)method2 { }

@end

pragma mark -

NSLog(@"测试objc_class中的方法列表是否包含分类中的方法"); unsigned int outCount = 0; Method *methodList = class_copyMethodList(RuntimeCategoryClass.class, &outCount);

for (int i = 0; i < outCount; i++) { Method method = methodList[i];

const char *name = sel_getName(method_getName(method));

NSLog(@"RuntimeCategoryClass's method: %s", name);

if (strcmp(name, sel_getName(@selector(method2)))) {
      NSLog(@"分类方法method2在objc_class的方法列表中");
}

}

//输出 2014-11-08 10:36:39.213 [561:151847] 测试objc_class中的方法列表是否包含分类中的方法 2014-11-08 10:36:39.215 [561:151847] RuntimeCategoryClass's method: method2 2014-11-08 10:36:39.215 [561:151847] RuntimeCategoryClass's method: method1 2014-11-08 10:36:39.215 [561:151847] 分类方法method2在objc_class的方法列表中

Runtime提供了Protocol的一系列函数操作,函数包括

// 返回指定的协议 Protocol objc_getProtocol ( const char name ); // 获取运行时所知道的所有协议的数组 Protocol objc_copyProtocolList ( unsigned int outCount ); // 创建新的协议实例 Protocol objc_allocateProtocol ( const char name ); // 在运行时中注册新创建的协议 void objc_registerProtocol ( Protocol proto ); //创建一个新协议后必须使用这个进行注册这个新协议,但是注册后不能够再修改和添加新方法。 // 为协议添加方法 void protocol_addMethodDescription ( Protocol proto, SEL name, const char types, BOOL isRequiredMethod, BOOL isInstanceMethod ); // 添加一个已注册的协议到协议中 void protocol_addProtocol ( Protocol proto, Protocol addition ); // 为协议添加属性 void protocol_addProperty ( Protocol proto, const char name, const objc_property_attribute_t attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty ); // 返回协议名 const char protocol_getName ( Protocol p ); // 测试两个协议是否相等 BOOL protocol_isEqual ( Protocol proto, Protocol other ); // 获取协议中指定条件的方法的方法描述数组 struct objc_method_description protocol_copyMethodDescriptionList ( Protocol p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int outCount ); // 获取协议中指定方法的方法描述 struct objc_method_description protocol_getMethodDescription ( Protocol p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod ); // 获取协议中的属性列表 objc_property_t protocol_copyPropertyList ( Protocol proto, unsigned int outCount ); // 获取协议的指定属性 objc_property_t protocol_getProperty ( Protocol proto, const char name, BOOL isRequiredProperty, BOOL isInstanceProperty ); // 获取协议采用的协议 Protocol protocol_copyProtocolList ( Protocol proto, unsigned int outCount ); // 查看协议是否采用了另一个协议 BOOL protocol_conformsToProtocol ( Protocol proto, Protocol other );

Block

runtime中一些支持block操作的函数

// 创建一个指针函数的指针,该函数调用时会调用特定的block IMP imp_implementationWithBlock ( id block );

// 返回与IMP(使用imp_implementationWithBlock创建的)相关的block id imp_getBlock ( IMP anImp );

// 解除block与IMP(使用imp_implementationWithBlock创建的)的关联关系,并释放block的拷贝 BOOL imp_removeBlock ( IMP anImp );

测试代码

@interface MyRuntimeBlock : NSObject

@end

@implementation MyRuntimeBlock

@end

IMP imp = imp_implementationWithBlock(^(id obj, NSString str) { NSLog(@"%@", str); }); class_addMethod(MyRuntimeBlock.class, @selector(testBlock:), imp, "v@:@"); MyRuntimeBlock runtime = [[MyRuntimeBlock alloc] init]; [runtime performSelector:@selector(testBlock:) withObject:@"hello world!"];

//结果 2014-11-09 14:03:19.779 [1172:395446] hello world!

Runtime的应用

获取系统提供的库相关信息

主要函数

// 获取所有加载的Objective-C框架和动态库的名称 const char * objc_copyImageNames ( unsigned int outCount );

// 获取指定类所在动态库 const char * class_getImageName ( Class cls );

// 获取指定库或框架中所有类的类名 const char * objc_copyClassNamesForImage ( const char image, unsigned int *outCount );

通过这些函数就能够获取某个类所有的库,以及某个库中包含哪些类

NSLog(@"获取指定类所在动态库");

NSLog(@"UIView's Framework: %s", class_getImageName(NSClassFromString(@"UIView")));

NSLog(@"获取指定库或框架中所有类的类名"); const char ** classes = objc_copyClassNamesForImage(class_getImageName(NSClassFromString(@"UIView")), &outCount); for (int i = 0; i < outCount; i++) { NSLog(@"class name: %s", classes[i]); }

//结果 2014-11-08 12:57:32.689 [747:184013] 获取指定类所在动态库 2014-11-08 12:57:32.690 [747:184013] UIView's Framework: /System/Library/Frameworks/UIKit.framework/UIKit 2014-11-08 12:57:32.690 [747:184013] 获取指定库或框架中所有类的类名 2014-11-08 12:57:32.691 [747:184013] class name: UIKeyboardPredictiveSettings 2014-11-08 12:57:32.691 [747:184013] class name: _UIPickerViewTopFrame 2014-11-08 12:57:32.691 [747:184013] class name: _UIOnePartImageView 2014-11-08 12:57:32.692 [747:184013] class name: _UIPickerViewSelectionBar 2014-11-08 12:57:32.692 [747:184013] class name: _UIPickerWheelView 2014-11-08 12:57:32.692 [747:184013] class name: _UIPickerViewTestParameters ......

对App的用户行为进行追踪

就是用户点击时把事件记录下来。一般比较做法就是在viewDidAppear里记录事件,这样会让这样记录事件的代码遍布整个项目中。继承或类别也会有问题。这时利用Method Swizzling把一个方法的实现和另一个方法的实现进行替换。

//先定义一个类别,添加要Swizzled的方法 @implementation UIViewController (Logging)- (void)swizzled_viewDidAppear:(BOOL)animated { // call original implementation [self swizzled_viewDidAppear:animated]; // Logging [Logging logWithEventName:NSStringFromClass([self class])]; } //接下来实现swizzle方法 @implementation UIViewController (Logging)void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector) { // the method might not exist in the class, but in its superclass Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); // class_addMethod will fail if original method already exists BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); // the method doesn’t exist and we just added one if (didAddMethod) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } }

//最后要确保在程序启动的时候调用swizzleMethod方法在之前的UIViewController的Logging类别里添加+load:方法,然后在+load:里把viewDidAppear替换掉 @implementation UIViewController (Logging)+ (void)load { swizzleMethod([self class], @selector(viewDidAppear:), @selector(swizzled_viewDidAppear:)); }

//更简化直接用新的IMP取代原IMP,不是替换,只需要有全局的函数指针指向原IMP即可。 void (gOriginalViewDidAppear)(id, SEL, BOOL);void newViewDidAppear(UIViewController *self, SEL _cmd, BOOL animated) { // call original implementation gOriginalViewDidAppear(self, _cmd, animated); // Logging [Logging logWithEventName:NSStringFromClass([self class])]; }

  • (void)load { Method originalMethod = class_getInstanceMethod(self, @selector(viewDidAppear:)); gOriginalViewDidAppear = (void *)method_getImplementation(originalMethod); if(!class_addMethod(self, @selector(viewDidAppear:), (IMP) newViewDidAppear, method_getTypeEncoding(originalMethod))) { method_setImplementation(originalMethod, (IMP) newViewDidAppear); } }

通过Method Swizzling可以把事件代码或Logging,Authentication,Caching等跟主要业务逻辑代码解耦。这种处理方式叫做Cross Cutting Concernshttp://en.wikipedia.org/wiki/Cross-cutting_concern 用Method Swizzling动态给指定的方法添加代码解决Cross Cutting Concerns的编程方式叫Aspect Oriented Programming http://en.wikipedia.org/wiki/Aspect-oriented_programming 目前有些第三方库可以很方便的使用AOP,比如Aspects https://github.com/steipete/Aspects 这里是使用Aspects的范例https://github.com/okcomp/AspectsDemo

收藏
2
sina weixin mail 回到顶部