-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSMutableArray+WDMutableArray.m
More file actions
36 lines (28 loc) · 1.16 KB
/
NSMutableArray+WDMutableArray.m
File metadata and controls
36 lines (28 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#import "NSMutableArray+WDMutableArray.h"
#import <objc/runtime.h>
@implementation NSMutableArray (WDMutableArray)
+ (void)load {
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(wd_objectAtIndex:));
method_exchangeImplementations(fromMethod, toMethod);
}
// 为了避免和系统的方法冲突,我一般都会在swizzling方法前面加前缀
- (id)wd_objectAtIndex:(NSUInteger)index {
// 判断下标是否越界,如果越界就进入异常拦截
if (self.count-1 < index) {
@try {
return [self wd_objectAtIndex:index];
}
@catch (NSException *exception) {
// 在崩溃后会打印崩溃信息。
NSLog(@"---------- %s Crash Because Method %s ----------\n", class_getName(self.class), __func__);
NSLog(@"%@", [exception callStackSymbols]);
return nil;
}
@finally {}
} // 如果没有问题,则正常进行方法调用
else {
return [self wd_objectAtIndex:index];
}
}
@end