NSAttributedString+AvoidCrash.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // NSAttributedString+AvoidCrash.m
  3. // https://github.com/chenfanfang/AvoidCrash
  4. //
  5. // Created by mac on 16/10/15.
  6. // Copyright © 2016年 chenfanfang. All rights reserved.
  7. //
  8. #import "NSAttributedString+AvoidCrash.h"
  9. #import "AvoidCrash.h"
  10. @implementation NSAttributedString (AvoidCrash)
  11. + (void)avoidCrashExchangeMethod {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. Class NSConcreteAttributedString = NSClassFromString(@"NSConcreteAttributedString");
  15. //initWithString:
  16. [AvoidCrash exchangeInstanceMethod:NSConcreteAttributedString method1Sel:@selector(initWithString:) method2Sel:@selector(avoidCrashInitWithString:)];
  17. //initWithAttributedString
  18. [AvoidCrash exchangeInstanceMethod:NSConcreteAttributedString method1Sel:@selector(initWithAttributedString:) method2Sel:@selector(avoidCrashInitWithAttributedString:)];
  19. //initWithString:attributes:
  20. [AvoidCrash exchangeInstanceMethod:NSConcreteAttributedString method1Sel:@selector(initWithString:attributes:) method2Sel:@selector(avoidCrashInitWithString:attributes:)];
  21. });
  22. }
  23. //=================================================================
  24. // initWithString:
  25. //=================================================================
  26. #pragma mark - initWithString:
  27. - (instancetype)avoidCrashInitWithString:(NSString *)str {
  28. id object = nil;
  29. @try {
  30. object = [self avoidCrashInitWithString:str];
  31. }
  32. @catch (NSException *exception) {
  33. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  34. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  35. }
  36. @finally {
  37. return object;
  38. }
  39. }
  40. //=================================================================
  41. // initWithAttributedString
  42. //=================================================================
  43. #pragma mark - initWithAttributedString
  44. - (instancetype)avoidCrashInitWithAttributedString:(NSAttributedString *)attrStr {
  45. id object = nil;
  46. @try {
  47. object = [self avoidCrashInitWithAttributedString:attrStr];
  48. }
  49. @catch (NSException *exception) {
  50. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  51. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  52. }
  53. @finally {
  54. return object;
  55. }
  56. }
  57. //=================================================================
  58. // initWithString:attributes:
  59. //=================================================================
  60. #pragma mark - initWithString:attributes:
  61. - (instancetype)avoidCrashInitWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs {
  62. id object = nil;
  63. @try {
  64. object = [self avoidCrashInitWithString:str attributes:attrs];
  65. }
  66. @catch (NSException *exception) {
  67. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  68. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  69. }
  70. @finally {
  71. return object;
  72. }
  73. }
  74. @end