123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- //
- // NSArray+AvoidCrash.m
- // https://github.com/chenfanfang/AvoidCrash
- //
- // Created by mac on 16/9/21.
- // Copyright © 2016年 chenfanfang. All rights reserved.
- //
- #import "NSArray+AvoidCrash.h"
- #import "AvoidCrash.h"
- @implementation NSArray (AvoidCrash)
- + (void)avoidCrashExchangeMethod {
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- //=================
- // class method
- //=================
-
- //instance array method exchange
- [AvoidCrash exchangeClassMethod:[self class] method1Sel:@selector(arrayWithObjects:count:) method2Sel:@selector(AvoidCrashArrayWithObjects:count:)];
-
-
-
- //====================
- // instance method
- //====================
-
- Class __NSArray = NSClassFromString(@"NSArray");
- Class __NSArrayI = NSClassFromString(@"__NSArrayI");
- Class __NSSingleObjectArrayI = NSClassFromString(@"__NSSingleObjectArrayI");
- Class __NSArray0 = NSClassFromString(@"__NSArray0");
-
-
- //objectsAtIndexes:
- [AvoidCrash exchangeInstanceMethod:__NSArray method1Sel:@selector(objectsAtIndexes:) method2Sel:@selector(avoidCrashObjectsAtIndexes:)];
-
-
- //objectAtIndex:
-
- [AvoidCrash exchangeInstanceMethod:__NSArrayI method1Sel:@selector(objectAtIndex:) method2Sel:@selector(__NSArrayIAvoidCrashObjectAtIndex:)];
-
- [AvoidCrash exchangeInstanceMethod:__NSSingleObjectArrayI method1Sel:@selector(objectAtIndex:) method2Sel:@selector(__NSSingleObjectArrayIAvoidCrashObjectAtIndex:)];
-
- [AvoidCrash exchangeInstanceMethod:__NSArray0 method1Sel:@selector(objectAtIndex:) method2Sel:@selector(__NSArray0AvoidCrashObjectAtIndex:)];
-
- //objectAtIndexedSubscript:
- if (AvoidCrashIsiOS(11.0)) {
- [AvoidCrash exchangeInstanceMethod:__NSArrayI method1Sel:@selector(objectAtIndexedSubscript:) method2Sel:@selector(__NSArrayIAvoidCrashObjectAtIndexedSubscript:)];
- }
-
-
- //getObjects:range:
- [AvoidCrash exchangeInstanceMethod:__NSArray method1Sel:@selector(getObjects:range:) method2Sel:@selector(NSArrayAvoidCrashGetObjects:range:)];
-
- [AvoidCrash exchangeInstanceMethod:__NSSingleObjectArrayI method1Sel:@selector(getObjects:range:) method2Sel:@selector(__NSSingleObjectArrayIAvoidCrashGetObjects:range:)];
-
- [AvoidCrash exchangeInstanceMethod:__NSArrayI method1Sel:@selector(getObjects:range:) method2Sel:@selector(__NSArrayIAvoidCrashGetObjects:range:)];
- });
-
-
- }
- //=================================================================
- // instance array
- //=================================================================
- #pragma mark - instance array
- + (instancetype)AvoidCrashArrayWithObjects:(const id _Nonnull __unsafe_unretained *)objects count:(NSUInteger)cnt {
-
- id instance = nil;
-
- @try {
- instance = [self AvoidCrashArrayWithObjects:objects count:cnt];
- }
- @catch (NSException *exception) {
-
- NSString *defaultToDo = @"AvoidCrash default is to remove nil object and instance a array.";
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
-
- //以下是对错误数据的处理,把为nil的数据去掉,然后初始化数组
- NSInteger newObjsIndex = 0;
- id _Nonnull __unsafe_unretained newObjects[cnt];
-
- for (int i = 0; i < cnt; i++) {
- if (objects[i] != nil) {
- newObjects[newObjsIndex] = objects[i];
- newObjsIndex++;
- }
- }
- instance = [self AvoidCrashArrayWithObjects:newObjects count:newObjsIndex];
- }
- @finally {
- return instance;
- }
- }
- //=================================================================
- // objectAtIndexedSubscript:
- //=================================================================
- #pragma mark - objectAtIndexedSubscript:
- - (id)__NSArrayIAvoidCrashObjectAtIndexedSubscript:(NSUInteger)idx {
-
- id object = nil;
-
- @try {
- object = [self __NSArrayIAvoidCrashObjectAtIndexedSubscript:idx];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
- }
- //=================================================================
- // objectsAtIndexes:
- //=================================================================
- #pragma mark - objectsAtIndexes:
- - (NSArray *)avoidCrashObjectsAtIndexes:(NSIndexSet *)indexes {
-
- NSArray *returnArray = nil;
- @try {
- returnArray = [self avoidCrashObjectsAtIndexes:indexes];
- } @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
-
- } @finally {
- return returnArray;
- }
- }
- //=================================================================
- // objectAtIndex:
- //=================================================================
- #pragma mark - objectAtIndex:
- //__NSArrayI objectAtIndex:
- - (id)__NSArrayIAvoidCrashObjectAtIndex:(NSUInteger)index {
- id object = nil;
-
- @try {
- object = [self __NSArrayIAvoidCrashObjectAtIndex:index];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
- }
- //__NSSingleObjectArrayI objectAtIndex:
- - (id)__NSSingleObjectArrayIAvoidCrashObjectAtIndex:(NSUInteger)index {
- id object = nil;
-
- @try {
- object = [self __NSSingleObjectArrayIAvoidCrashObjectAtIndex:index];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
- }
- //__NSArray0 objectAtIndex:
- - (id)__NSArray0AvoidCrashObjectAtIndex:(NSUInteger)index {
- id object = nil;
-
- @try {
- object = [self __NSArray0AvoidCrashObjectAtIndex:index];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
- }
- //=================================================================
- // getObjects:range:
- //=================================================================
- #pragma mark - getObjects:range:
- //NSArray getObjects:range:
- - (void)NSArrayAvoidCrashGetObjects:(__unsafe_unretained id _Nonnull *)objects range:(NSRange)range {
-
- @try {
- [self NSArrayAvoidCrashGetObjects:objects range:range];
- } @catch (NSException *exception) {
-
- NSString *defaultToDo = AvoidCrashDefaultIgnore;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
-
- } @finally {
-
- }
- }
- //__NSSingleObjectArrayI getObjects:range:
- - (void)__NSSingleObjectArrayIAvoidCrashGetObjects:(__unsafe_unretained id _Nonnull *)objects range:(NSRange)range {
-
- @try {
- [self __NSSingleObjectArrayIAvoidCrashGetObjects:objects range:range];
- } @catch (NSException *exception) {
-
- NSString *defaultToDo = AvoidCrashDefaultIgnore;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
-
- } @finally {
- //x * 0.17 = 3000
- }
-
- }
- //__NSArrayI getObjects:range:
- - (void)__NSArrayIAvoidCrashGetObjects:(__unsafe_unretained id _Nonnull *)objects range:(NSRange)range {
-
- @try {
- [self __NSArrayIAvoidCrashGetObjects:objects range:range];
- } @catch (NSException *exception) {
-
- NSString *defaultToDo = AvoidCrashDefaultIgnore;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
-
- } @finally {
-
- }
-
- }
- @end
|