NSString+AvoidCrash.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // NSString+AvoidCrash.m
  3. // https://github.com/chenfanfang/AvoidCrash
  4. //
  5. // Created by mac on 16/10/5.
  6. // Copyright © 2016年 chenfanfang. All rights reserved.
  7. //
  8. #import "NSString+AvoidCrash.h"
  9. #import "AvoidCrash.h"
  10. @implementation NSString (AvoidCrash)
  11. + (void)avoidCrashExchangeMethod {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. Class stringClass = NSClassFromString(@"__NSCFConstantString");
  15. //characterAtIndex
  16. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(characterAtIndex:) method2Sel:@selector(avoidCrashCharacterAtIndex:)];
  17. //substringFromIndex
  18. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(substringFromIndex:) method2Sel:@selector(avoidCrashSubstringFromIndex:)];
  19. //substringToIndex
  20. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(substringToIndex:) method2Sel:@selector(avoidCrashSubstringToIndex:)];
  21. //substringWithRange:
  22. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(substringWithRange:) method2Sel:@selector(avoidCrashSubstringWithRange:)];
  23. //stringByReplacingOccurrencesOfString:
  24. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(stringByReplacingOccurrencesOfString:withString:) method2Sel:@selector(avoidCrashStringByReplacingOccurrencesOfString:withString:)];
  25. //stringByReplacingOccurrencesOfString:withString:options:range:
  26. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(stringByReplacingOccurrencesOfString:withString:options:range:) method2Sel:@selector(avoidCrashStringByReplacingOccurrencesOfString:withString:options:range:)];
  27. //stringByReplacingCharactersInRange:withString:
  28. [AvoidCrash exchangeInstanceMethod:stringClass method1Sel:@selector(stringByReplacingCharactersInRange:withString:) method2Sel:@selector(avoidCrashStringByReplacingCharactersInRange:withString:)];
  29. });
  30. }
  31. //=================================================================
  32. // characterAtIndex:
  33. //=================================================================
  34. #pragma mark - characterAtIndex:
  35. - (unichar)avoidCrashCharacterAtIndex:(NSUInteger)index {
  36. unichar characteristic;
  37. @try {
  38. characteristic = [self avoidCrashCharacterAtIndex:index];
  39. }
  40. @catch (NSException *exception) {
  41. NSString *defaultToDo = @"AvoidCrash default is to return a without assign unichar.";
  42. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  43. }
  44. @finally {
  45. return characteristic;
  46. }
  47. }
  48. //=================================================================
  49. // substringFromIndex:
  50. //=================================================================
  51. #pragma mark - substringFromIndex:
  52. - (NSString *)avoidCrashSubstringFromIndex:(NSUInteger)from {
  53. NSString *subString = nil;
  54. @try {
  55. subString = [self avoidCrashSubstringFromIndex:from];
  56. }
  57. @catch (NSException *exception) {
  58. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  59. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  60. subString = nil;
  61. }
  62. @finally {
  63. return subString;
  64. }
  65. }
  66. //=================================================================
  67. // substringToIndex
  68. //=================================================================
  69. #pragma mark - substringToIndex
  70. - (NSString *)avoidCrashSubstringToIndex:(NSUInteger)to {
  71. NSString *subString = nil;
  72. @try {
  73. subString = [self avoidCrashSubstringToIndex:to];
  74. }
  75. @catch (NSException *exception) {
  76. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  77. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  78. subString = nil;
  79. }
  80. @finally {
  81. return subString;
  82. }
  83. }
  84. //=================================================================
  85. // substringWithRange:
  86. //=================================================================
  87. #pragma mark - substringWithRange:
  88. - (NSString *)avoidCrashSubstringWithRange:(NSRange)range {
  89. NSString *subString = nil;
  90. @try {
  91. subString = [self avoidCrashSubstringWithRange:range];
  92. }
  93. @catch (NSException *exception) {
  94. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  95. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  96. subString = nil;
  97. }
  98. @finally {
  99. return subString;
  100. }
  101. }
  102. //=================================================================
  103. // stringByReplacingOccurrencesOfString:
  104. //=================================================================
  105. #pragma mark - stringByReplacingOccurrencesOfString:
  106. - (NSString *)avoidCrashStringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement {
  107. NSString *newStr = nil;
  108. @try {
  109. newStr = [self avoidCrashStringByReplacingOccurrencesOfString:target withString:replacement];
  110. }
  111. @catch (NSException *exception) {
  112. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  113. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  114. newStr = nil;
  115. }
  116. @finally {
  117. return newStr;
  118. }
  119. }
  120. //=================================================================
  121. // stringByReplacingOccurrencesOfString:withString:options:range:
  122. //=================================================================
  123. #pragma mark - stringByReplacingOccurrencesOfString:withString:options:range:
  124. - (NSString *)avoidCrashStringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange {
  125. NSString *newStr = nil;
  126. @try {
  127. newStr = [self avoidCrashStringByReplacingOccurrencesOfString:target withString:replacement options:options range:searchRange];
  128. }
  129. @catch (NSException *exception) {
  130. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  131. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  132. newStr = nil;
  133. }
  134. @finally {
  135. return newStr;
  136. }
  137. }
  138. //=================================================================
  139. // stringByReplacingCharactersInRange:withString:
  140. //=================================================================
  141. #pragma mark - stringByReplacingCharactersInRange:withString:
  142. - (NSString *)avoidCrashStringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement {
  143. NSString *newStr = nil;
  144. @try {
  145. newStr = [self avoidCrashStringByReplacingCharactersInRange:range withString:replacement];
  146. }
  147. @catch (NSException *exception) {
  148. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  149. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  150. newStr = nil;
  151. }
  152. @finally {
  153. return newStr;
  154. }
  155. }
  156. @end