NSArray+AvoidCrash.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // NSArray+AvoidCrash.m
  3. // https://github.com/chenfanfang/AvoidCrash
  4. //
  5. // Created by mac on 16/9/21.
  6. // Copyright © 2016年 chenfanfang. All rights reserved.
  7. //
  8. #import "NSArray+AvoidCrash.h"
  9. #import "AvoidCrash.h"
  10. @implementation NSArray (AvoidCrash)
  11. + (void)avoidCrashExchangeMethod {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. //=================
  15. // class method
  16. //=================
  17. //instance array method exchange
  18. [AvoidCrash exchangeClassMethod:[self class] method1Sel:@selector(arrayWithObjects:count:) method2Sel:@selector(AvoidCrashArrayWithObjects:count:)];
  19. //====================
  20. // instance method
  21. //====================
  22. Class __NSArray = NSClassFromString(@"NSArray");
  23. Class __NSArrayI = NSClassFromString(@"__NSArrayI");
  24. Class __NSSingleObjectArrayI = NSClassFromString(@"__NSSingleObjectArrayI");
  25. Class __NSArray0 = NSClassFromString(@"__NSArray0");
  26. //objectsAtIndexes:
  27. [AvoidCrash exchangeInstanceMethod:__NSArray method1Sel:@selector(objectsAtIndexes:) method2Sel:@selector(avoidCrashObjectsAtIndexes:)];
  28. //objectAtIndex:
  29. [AvoidCrash exchangeInstanceMethod:__NSArrayI method1Sel:@selector(objectAtIndex:) method2Sel:@selector(__NSArrayIAvoidCrashObjectAtIndex:)];
  30. [AvoidCrash exchangeInstanceMethod:__NSSingleObjectArrayI method1Sel:@selector(objectAtIndex:) method2Sel:@selector(__NSSingleObjectArrayIAvoidCrashObjectAtIndex:)];
  31. [AvoidCrash exchangeInstanceMethod:__NSArray0 method1Sel:@selector(objectAtIndex:) method2Sel:@selector(__NSArray0AvoidCrashObjectAtIndex:)];
  32. //objectAtIndexedSubscript:
  33. if (AvoidCrashIsiOS(11.0)) {
  34. [AvoidCrash exchangeInstanceMethod:__NSArrayI method1Sel:@selector(objectAtIndexedSubscript:) method2Sel:@selector(__NSArrayIAvoidCrashObjectAtIndexedSubscript:)];
  35. }
  36. //getObjects:range:
  37. [AvoidCrash exchangeInstanceMethod:__NSArray method1Sel:@selector(getObjects:range:) method2Sel:@selector(NSArrayAvoidCrashGetObjects:range:)];
  38. [AvoidCrash exchangeInstanceMethod:__NSSingleObjectArrayI method1Sel:@selector(getObjects:range:) method2Sel:@selector(__NSSingleObjectArrayIAvoidCrashGetObjects:range:)];
  39. [AvoidCrash exchangeInstanceMethod:__NSArrayI method1Sel:@selector(getObjects:range:) method2Sel:@selector(__NSArrayIAvoidCrashGetObjects:range:)];
  40. });
  41. }
  42. //=================================================================
  43. // instance array
  44. //=================================================================
  45. #pragma mark - instance array
  46. + (instancetype)AvoidCrashArrayWithObjects:(const id _Nonnull __unsafe_unretained *)objects count:(NSUInteger)cnt {
  47. id instance = nil;
  48. @try {
  49. instance = [self AvoidCrashArrayWithObjects:objects count:cnt];
  50. }
  51. @catch (NSException *exception) {
  52. NSString *defaultToDo = @"AvoidCrash default is to remove nil object and instance a array.";
  53. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  54. //以下是对错误数据的处理,把为nil的数据去掉,然后初始化数组
  55. NSInteger newObjsIndex = 0;
  56. id _Nonnull __unsafe_unretained newObjects[cnt];
  57. for (int i = 0; i < cnt; i++) {
  58. if (objects[i] != nil) {
  59. newObjects[newObjsIndex] = objects[i];
  60. newObjsIndex++;
  61. }
  62. }
  63. instance = [self AvoidCrashArrayWithObjects:newObjects count:newObjsIndex];
  64. }
  65. @finally {
  66. return instance;
  67. }
  68. }
  69. //=================================================================
  70. // objectAtIndexedSubscript:
  71. //=================================================================
  72. #pragma mark - objectAtIndexedSubscript:
  73. - (id)__NSArrayIAvoidCrashObjectAtIndexedSubscript:(NSUInteger)idx {
  74. id object = nil;
  75. @try {
  76. object = [self __NSArrayIAvoidCrashObjectAtIndexedSubscript:idx];
  77. }
  78. @catch (NSException *exception) {
  79. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  80. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  81. }
  82. @finally {
  83. return object;
  84. }
  85. }
  86. //=================================================================
  87. // objectsAtIndexes:
  88. //=================================================================
  89. #pragma mark - objectsAtIndexes:
  90. - (NSArray *)avoidCrashObjectsAtIndexes:(NSIndexSet *)indexes {
  91. NSArray *returnArray = nil;
  92. @try {
  93. returnArray = [self avoidCrashObjectsAtIndexes:indexes];
  94. } @catch (NSException *exception) {
  95. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  96. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  97. } @finally {
  98. return returnArray;
  99. }
  100. }
  101. //=================================================================
  102. // objectAtIndex:
  103. //=================================================================
  104. #pragma mark - objectAtIndex:
  105. //__NSArrayI objectAtIndex:
  106. - (id)__NSArrayIAvoidCrashObjectAtIndex:(NSUInteger)index {
  107. id object = nil;
  108. @try {
  109. object = [self __NSArrayIAvoidCrashObjectAtIndex:index];
  110. }
  111. @catch (NSException *exception) {
  112. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  113. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  114. }
  115. @finally {
  116. return object;
  117. }
  118. }
  119. //__NSSingleObjectArrayI objectAtIndex:
  120. - (id)__NSSingleObjectArrayIAvoidCrashObjectAtIndex:(NSUInteger)index {
  121. id object = nil;
  122. @try {
  123. object = [self __NSSingleObjectArrayIAvoidCrashObjectAtIndex:index];
  124. }
  125. @catch (NSException *exception) {
  126. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  127. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  128. }
  129. @finally {
  130. return object;
  131. }
  132. }
  133. //__NSArray0 objectAtIndex:
  134. - (id)__NSArray0AvoidCrashObjectAtIndex:(NSUInteger)index {
  135. id object = nil;
  136. @try {
  137. object = [self __NSArray0AvoidCrashObjectAtIndex:index];
  138. }
  139. @catch (NSException *exception) {
  140. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  141. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  142. }
  143. @finally {
  144. return object;
  145. }
  146. }
  147. //=================================================================
  148. // getObjects:range:
  149. //=================================================================
  150. #pragma mark - getObjects:range:
  151. //NSArray getObjects:range:
  152. - (void)NSArrayAvoidCrashGetObjects:(__unsafe_unretained id _Nonnull *)objects range:(NSRange)range {
  153. @try {
  154. [self NSArrayAvoidCrashGetObjects:objects range:range];
  155. } @catch (NSException *exception) {
  156. NSString *defaultToDo = AvoidCrashDefaultIgnore;
  157. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  158. } @finally {
  159. }
  160. }
  161. //__NSSingleObjectArrayI getObjects:range:
  162. - (void)__NSSingleObjectArrayIAvoidCrashGetObjects:(__unsafe_unretained id _Nonnull *)objects range:(NSRange)range {
  163. @try {
  164. [self __NSSingleObjectArrayIAvoidCrashGetObjects:objects range:range];
  165. } @catch (NSException *exception) {
  166. NSString *defaultToDo = AvoidCrashDefaultIgnore;
  167. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  168. } @finally {
  169. //x * 0.17 = 3000
  170. }
  171. }
  172. //__NSArrayI getObjects:range:
  173. - (void)__NSArrayIAvoidCrashGetObjects:(__unsafe_unretained id _Nonnull *)objects range:(NSRange)range {
  174. @try {
  175. [self __NSArrayIAvoidCrashGetObjects:objects range:range];
  176. } @catch (NSException *exception) {
  177. NSString *defaultToDo = AvoidCrashDefaultIgnore;
  178. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  179. } @finally {
  180. }
  181. }
  182. @end