NSMutableArray+AvoidCrash.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // NSMutableArray+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 "NSMutableArray+AvoidCrash.h"
  9. #import "AvoidCrash.h"
  10. @implementation NSMutableArray (AvoidCrash)
  11. + (void)avoidCrashExchangeMethod {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. Class arrayMClass = NSClassFromString(@"__NSArrayM");
  15. //objectAtIndex:
  16. [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(objectAtIndex:) method2Sel:@selector(avoidCrashObjectAtIndex:)];
  17. //objectAtIndexedSubscript
  18. if (AvoidCrashIsiOS(11.0)) {
  19. [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(objectAtIndexedSubscript:) method2Sel:@selector(avoidCrashObjectAtIndexedSubscript:)];
  20. }
  21. //setObject:atIndexedSubscript:
  22. [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(setObject:atIndexedSubscript:) method2Sel:@selector(avoidCrashSetObject:atIndexedSubscript:)];
  23. //removeObjectAtIndex:
  24. [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(removeObjectAtIndex:) method2Sel:@selector(avoidCrashRemoveObjectAtIndex:)];
  25. //insertObject:atIndex:
  26. [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(insertObject:atIndex:) method2Sel:@selector(avoidCrashInsertObject:atIndex:)];
  27. //getObjects:range:
  28. [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(getObjects:range:) method2Sel:@selector(avoidCrashGetObjects:range:)];
  29. });
  30. }
  31. //=================================================================
  32. // array set object at index
  33. //=================================================================
  34. #pragma mark - get object from array
  35. - (void)avoidCrashSetObject:(id)obj atIndexedSubscript:(NSUInteger)idx {
  36. @try {
  37. [self avoidCrashSetObject:obj atIndexedSubscript:idx];
  38. }
  39. @catch (NSException *exception) {
  40. [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
  41. }
  42. @finally {
  43. }
  44. }
  45. //=================================================================
  46. // removeObjectAtIndex:
  47. //=================================================================
  48. #pragma mark - removeObjectAtIndex:
  49. - (void)avoidCrashRemoveObjectAtIndex:(NSUInteger)index {
  50. @try {
  51. [self avoidCrashRemoveObjectAtIndex:index];
  52. }
  53. @catch (NSException *exception) {
  54. [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
  55. }
  56. @finally {
  57. }
  58. }
  59. //=================================================================
  60. // insertObject:atIndex:
  61. //=================================================================
  62. #pragma mark - set方法
  63. - (void)avoidCrashInsertObject:(id)anObject atIndex:(NSUInteger)index {
  64. @try {
  65. [self avoidCrashInsertObject:anObject atIndex:index];
  66. }
  67. @catch (NSException *exception) {
  68. [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
  69. }
  70. @finally {
  71. }
  72. }
  73. //=================================================================
  74. // objectAtIndex:
  75. //=================================================================
  76. #pragma mark - objectAtIndex:
  77. - (id)avoidCrashObjectAtIndex:(NSUInteger)index {
  78. id object = nil;
  79. @try {
  80. object = [self avoidCrashObjectAtIndex:index];
  81. }
  82. @catch (NSException *exception) {
  83. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  84. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  85. }
  86. @finally {
  87. return object;
  88. }
  89. }
  90. //=================================================================
  91. // objectAtIndexedSubscript:
  92. //=================================================================
  93. #pragma mark - objectAtIndexedSubscript:
  94. - (id)avoidCrashObjectAtIndexedSubscript:(NSUInteger)idx {
  95. id object = nil;
  96. @try {
  97. object = [self avoidCrashObjectAtIndexedSubscript:idx];
  98. }
  99. @catch (NSException *exception) {
  100. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  101. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  102. }
  103. @finally {
  104. return object;
  105. }
  106. }
  107. //=================================================================
  108. // getObjects:range:
  109. //=================================================================
  110. #pragma mark - getObjects:range:
  111. - (void)avoidCrashGetObjects:(__unsafe_unretained id _Nonnull *)objects range:(NSRange)range {
  112. @try {
  113. [self avoidCrashGetObjects:objects range:range];
  114. } @catch (NSException *exception) {
  115. NSString *defaultToDo = AvoidCrashDefaultIgnore;
  116. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  117. } @finally {
  118. }
  119. }
  120. @end