NSMutableString+AvoidCrash.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // NSMutableString+AvoidCrash.m
  3. // https://github.com/chenfanfang/AvoidCrash
  4. //
  5. // Created by mac on 16/10/6.
  6. // Copyright © 2016年 chenfanfang. All rights reserved.
  7. //
  8. #import "NSMutableString+AvoidCrash.h"
  9. #import "AvoidCrash.h"
  10. @implementation NSMutableString (AvoidCrash)
  11. + (void)avoidCrashExchangeMethod {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. Class stringClass = NSClassFromString(@"__NSCFString");
  15. //replaceCharactersInRange
  16. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(replaceCharactersInRange:withString:) method2Sel:@selector(avoidCrashReplaceCharactersInRange:withString:)];
  17. //insertString:atIndex:
  18. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(insertString:atIndex:) method2Sel:@selector(avoidCrashInsertString:atIndex:)];
  19. //deleteCharactersInRange
  20. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(deleteCharactersInRange:) method2Sel:@selector(avoidCrashDeleteCharactersInRange:)];
  21. });
  22. }
  23. //=================================================================
  24. // replaceCharactersInRange
  25. //=================================================================
  26. #pragma mark - replaceCharactersInRange
  27. - (void)avoidCrashReplaceCharactersInRange:(NSRange)range withString:(NSString *)aString {
  28. @try {
  29. [self avoidCrashReplaceCharactersInRange:range withString:aString];
  30. }
  31. @catch (NSException *exception) {
  32. NSString *defaultToDo = AvoidCrashDefaultIgnore;
  33. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  34. }
  35. @finally {
  36. }
  37. }
  38. //=================================================================
  39. // insertString:atIndex:
  40. //=================================================================
  41. #pragma mark - insertString:atIndex:
  42. - (void)avoidCrashInsertString:(NSString *)aString atIndex:(NSUInteger)loc {
  43. @try {
  44. [self avoidCrashInsertString:aString atIndex:loc];
  45. }
  46. @catch (NSException *exception) {
  47. NSString *defaultToDo = AvoidCrashDefaultIgnore;
  48. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  49. }
  50. @finally {
  51. }
  52. }
  53. //=================================================================
  54. // deleteCharactersInRange
  55. //=================================================================
  56. #pragma mark - deleteCharactersInRange
  57. - (void)avoidCrashDeleteCharactersInRange:(NSRange)range {
  58. @try {
  59. [self avoidCrashDeleteCharactersInRange:range];
  60. }
  61. @catch (NSException *exception) {
  62. NSString *defaultToDo = AvoidCrashDefaultIgnore;
  63. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  64. }
  65. @finally {
  66. }
  67. }
  68. @end