1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- //
- // NSMutableDictionary+AvoidCrash.m
- // https://github.com/chenfanfang/AvoidCrash
- //
- // Created by mac on 16/9/22.
- // Copyright © 2016年 chenfanfang. All rights reserved.
- //
- #import "NSMutableDictionary+AvoidCrash.h"
- #import "AvoidCrash.h"
- @implementation NSMutableDictionary (AvoidCrash)
- + (void)avoidCrashExchangeMethod {
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- Class dictionaryM = NSClassFromString(@"__NSDictionaryM");
-
- //setObject:forKey:
- [AvoidCrash exchangeInstanceMethod:dictionaryM method1Sel:@selector(setObject:forKey:) method2Sel:@selector(avoidCrashSetObject:forKey:)];
-
- //setObject:forKeyedSubscript:
- if (AvoidCrashIsiOS(11.0)) {
- [AvoidCrash exchangeInstanceMethod:dictionaryM method1Sel:@selector(setObject:forKeyedSubscript:) method2Sel:@selector(avoidCrashSetObject:forKeyedSubscript:)];
- }
-
-
-
- //removeObjectForKey:
- Method removeObjectForKey = class_getInstanceMethod(dictionaryM, @selector(removeObjectForKey:));
- Method avoidCrashRemoveObjectForKey = class_getInstanceMethod(dictionaryM, @selector(avoidCrashRemoveObjectForKey:));
- method_exchangeImplementations(removeObjectForKey, avoidCrashRemoveObjectForKey);
- });
- }
- //=================================================================
- // setObject:forKey:
- //=================================================================
- #pragma mark - setObject:forKey:
- - (void)avoidCrashSetObject:(id)anObject forKey:(id<NSCopying>)aKey {
-
- @try {
- [self avoidCrashSetObject:anObject forKey:aKey];
- }
- @catch (NSException *exception) {
- [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
- }
- @finally {
-
- }
- }
- //=================================================================
- // setObject:forKeyedSubscript:
- //=================================================================
- #pragma mark - setObject:forKeyedSubscript:
- - (void)avoidCrashSetObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
- @try {
- [self avoidCrashSetObject:obj forKeyedSubscript:key];
- }
- @catch (NSException *exception) {
- [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
- }
- @finally {
-
- }
- }
- //=================================================================
- // removeObjectForKey:
- //=================================================================
- #pragma mark - removeObjectForKey:
- - (void)avoidCrashRemoveObjectForKey:(id)aKey {
-
- @try {
- [self avoidCrashRemoveObjectForKey:aKey];
- }
- @catch (NSException *exception) {
- [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
- }
- @finally {
-
- }
- }
- @end
|