我是在NSMutableArray中做到的:
+load{
Method fromMethod1 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(addObject:));
Method toMethod1 = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(ww_addObject:));
method_exchangeImplementations(fromMethod1, toMethod1);
}
static int number = 0;
-(void)ww_addObject:(id)anObject
{
NSLog(@"ww_addObject run as :%d times ",number ++);
if (anObject == nil) {
return;
}
[self ww_addObject:anObject];
}
然后在demo中运行它,像这样记录:
209901] ww_addObject run as :1076 times
2016-12-22 15:40:00.071 testArr[12062:209901] ww_addObject run as :1077 times
2016-12-22 15:40:00.071 testArr[12062:209901] ww_addObject run as :1078 times
为什么它运行1078次???
我的测试代码:
NSMutableArray *arr = [@[@"11",@"22",@"33"] mutableCopy];
[arr addObject:nil];