swizzling
应该只在+load
中完成。
在 Objective-C 的运行时中,每个类有两个方法都会自动调用。
1、+load
是在一个类被初始装载时调用;
2、+initialize
是在应用第一次调用该类的类方法或实例方法前调用的。
两个方法都是可选的,并且只有在方法被实现的情况下才会被调用。
swizzling
应该只在dispatch_once
中完成,由于swizzling
改变了全局的状态,所以我们需要确保每个预防措施在运行时都是可用的。
原子操作就是这样一个用于确保代码只会被执行一次的预防措施,就算是在不同的线程中也能确保代码只执行一次。
Grand Central Dispatch
的dispatch_once
满足了所需要的需求,并且应该被当做使用swizzling
的初始化单例方法的标准。
代码示例
接口文件.h
#import <Foundation/Foundation.h>
@interface NSMutableArray (RunTime)
@end
实现文件.m
#import "NSMutableArray+RunTime.h"
#import <objc/runtime.h>
@implementation NSMutableArray (RunTime)
#pragma mark - 方法转换
+ (void)load
{
// 方法1
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 无效
// Class class = [self class];
// 有效
Class class = NSClassFromString(@"__NSArrayM");
SEL originalSelector = @selector(addObject:);
SEL swizzledSelector = @selector(addObjectSafe:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
// judge the method named swizzledMethod is already existed.
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
// if swizzledMethod is already existed.
if (didAddMethod)
{
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}
else
{
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
// 方法2
// 无效(异常:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil')
// Class class = [self class];
// 有效
// Class class = NSClassFromString(@"__NSArrayM");
// Method oldMethod = class_getInstanceMethod(class, @selector(addObject:));
// Method newMethod = class_getInstanceMethod(class, @selector(addObjectSafe:));
// method_exchangeImplementations(oldMethod, newMethod);
}
- (void)addObjectSafe:(id)anObject
{
if (anObject)
{
[self addObjectSafe:anObject];
}
}
@end
使用
#import "ViewController.h"
// 导入头文件
#import "NSMutableArray+RunTime.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"runtime";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"click" style:UIBarButtonItemStyleDone target:self action:@selector(buttonClick)];
// 方法转换
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1", @"2", nil];
NSLog(@"1 array: %@", array);
[array addObject:@"devZhang"];
NSLog(@"2 array: %@", array);
NSString *item = nil;
[array addObject:item]; // 执行该方法时不会闪退
NSLog(@"3 array: %@", array);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end