123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- //
- // NSMutableArray+AvoidCrash.m
- // https://github.com/chenfanfang/AvoidCrash
- //
- // Created by mac on 16/9/21.
- // Copyright © 2016年 chenfanfang. All rights reserved.
- //
- #import "NSMutableArray+AvoidCrash.h"
- #import "AvoidCrash.h"
- @implementation NSMutableArray (AvoidCrash)
- + (void)avoidCrashExchangeMethod {
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
-
- Class arrayMClass = NSClassFromString(@"__NSArrayM");
-
-
- //objectAtIndex:
- [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(objectAtIndex:) method2Sel:@selector(avoidCrashObjectAtIndex:)];
-
- //objectAtIndexedSubscript
- if (AvoidCrashIsiOS(11.0)) {
- [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(objectAtIndexedSubscript:) method2Sel:@selector(avoidCrashObjectAtIndexedSubscript:)];
- }
-
-
- //setObject:atIndexedSubscript:
- [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(setObject:atIndexedSubscript:) method2Sel:@selector(avoidCrashSetObject:atIndexedSubscript:)];
-
-
- //removeObjectAtIndex:
- [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(removeObjectAtIndex:) method2Sel:@selector(avoidCrashRemoveObjectAtIndex:)];
-
- //insertObject:atIndex:
- [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(insertObject:atIndex:) method2Sel:@selector(avoidCrashInsertObject:atIndex:)];
-
- //getObjects:range:
- [AvoidCrash exchangeInstanceMethod:arrayMClass method1Sel:@selector(getObjects:range:) method2Sel:@selector(avoidCrashGetObjects:range:)];
- });
-
-
-
- }
- //=================================================================
- // array set object at index
- //=================================================================
- #pragma mark - get object from array
- - (void)avoidCrashSetObject:(id)obj atIndexedSubscript:(NSUInteger)idx {
-
- @try {
- [self avoidCrashSetObject:obj atIndexedSubscript:idx];
- }
- @catch (NSException *exception) {
- [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
- }
- @finally {
-
- }
- }
- //=================================================================
- // removeObjectAtIndex:
- //=================================================================
- #pragma mark - removeObjectAtIndex:
- - (void)avoidCrashRemoveObjectAtIndex:(NSUInteger)index {
- @try {
- [self avoidCrashRemoveObjectAtIndex:index];
- }
- @catch (NSException *exception) {
- [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
- }
- @finally {
-
- }
- }
- //=================================================================
- // insertObject:atIndex:
- //=================================================================
- #pragma mark - set方法
- - (void)avoidCrashInsertObject:(id)anObject atIndex:(NSUInteger)index {
- @try {
- [self avoidCrashInsertObject:anObject atIndex:index];
- }
- @catch (NSException *exception) {
- [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
- }
- @finally {
-
- }
- }
- //=================================================================
- // objectAtIndex:
- //=================================================================
- #pragma mark - objectAtIndex:
- - (id)avoidCrashObjectAtIndex:(NSUInteger)index {
- id object = nil;
-
- @try {
- object = [self avoidCrashObjectAtIndex:index];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
- }
- //=================================================================
- // objectAtIndexedSubscript:
- //=================================================================
- #pragma mark - objectAtIndexedSubscript:
- - (id)avoidCrashObjectAtIndexedSubscript:(NSUInteger)idx {
- id object = nil;
-
- @try {
- object = [self avoidCrashObjectAtIndexedSubscript:idx];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
-
- }
- //=================================================================
- // getObjects:range:
- //=================================================================
- #pragma mark - getObjects:range:
- - (void)avoidCrashGetObjects:(__unsafe_unretained id _Nonnull *)objects range:(NSRange)range {
-
- @try {
- [self avoidCrashGetObjects:objects range:range];
- } @catch (NSException *exception) {
-
- NSString *defaultToDo = AvoidCrashDefaultIgnore;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
-
- } @finally {
-
- }
- }
- @end
|