NSMutableDictionary+AvoidCrash.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // NSMutableDictionary+AvoidCrash.m
  3. // https://github.com/chenfanfang/AvoidCrash
  4. //
  5. // Created by mac on 16/9/22.
  6. // Copyright © 2016年 chenfanfang. All rights reserved.
  7. //
  8. #import "NSMutableDictionary+AvoidCrash.h"
  9. #import "AvoidCrash.h"
  10. @implementation NSMutableDictionary (AvoidCrash)
  11. + (void)avoidCrashExchangeMethod {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. Class dictionaryM = NSClassFromString(@"__NSDictionaryM");
  15. //setObject:forKey:
  16. [AvoidCrash exchangeInstanceMethod:dictionaryM method1Sel:@selector(setObject:forKey:) method2Sel:@selector(avoidCrashSetObject:forKey:)];
  17. //setObject:forKeyedSubscript:
  18. if (AvoidCrashIsiOS(11.0)) {
  19. [AvoidCrash exchangeInstanceMethod:dictionaryM method1Sel:@selector(setObject:forKeyedSubscript:) method2Sel:@selector(avoidCrashSetObject:forKeyedSubscript:)];
  20. }
  21. //removeObjectForKey:
  22. Method removeObjectForKey = class_getInstanceMethod(dictionaryM, @selector(removeObjectForKey:));
  23. Method avoidCrashRemoveObjectForKey = class_getInstanceMethod(dictionaryM, @selector(avoidCrashRemoveObjectForKey:));
  24. method_exchangeImplementations(removeObjectForKey, avoidCrashRemoveObjectForKey);
  25. });
  26. }
  27. //=================================================================
  28. // setObject:forKey:
  29. //=================================================================
  30. #pragma mark - setObject:forKey:
  31. - (void)avoidCrashSetObject:(id)anObject forKey:(id<NSCopying>)aKey {
  32. @try {
  33. [self avoidCrashSetObject:anObject forKey:aKey];
  34. }
  35. @catch (NSException *exception) {
  36. [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
  37. }
  38. @finally {
  39. }
  40. }
  41. //=================================================================
  42. // setObject:forKeyedSubscript:
  43. //=================================================================
  44. #pragma mark - setObject:forKeyedSubscript:
  45. - (void)avoidCrashSetObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  46. @try {
  47. [self avoidCrashSetObject:obj forKeyedSubscript:key];
  48. }
  49. @catch (NSException *exception) {
  50. [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
  51. }
  52. @finally {
  53. }
  54. }
  55. //=================================================================
  56. // removeObjectForKey:
  57. //=================================================================
  58. #pragma mark - removeObjectForKey:
  59. - (void)avoidCrashRemoveObjectForKey:(id)aKey {
  60. @try {
  61. [self avoidCrashRemoveObjectForKey:aKey];
  62. }
  63. @catch (NSException *exception) {
  64. [AvoidCrash noteErrorWithException:exception defaultToDo:AvoidCrashDefaultIgnore];
  65. }
  66. @finally {
  67. }
  68. }
  69. @end