NSMutableAttributedString+AvoidCrash.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // NSMutableAttributedString+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 "NSMutableAttributedString+AvoidCrash.h"
  9. #import "AvoidCrash.h"
  10. @implementation NSMutableAttributedString (AvoidCrash)
  11. + (void)avoidCrashExchangeMethod {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. Class NSConcreteMutableAttributedString = NSClassFromString(@"NSConcreteMutableAttributedString");
  15. //initWithString:
  16. [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(initWithString:) method2Sel:@selector(avoidCrashInitWithString:)];
  17. //initWithString:attributes:
  18. [AvoidCrash exchangeInstanceMethod:NSConcreteMutableAttributedString method1Sel:@selector(initWithString:attributes:) method2Sel:@selector(avoidCrashInitWithString:attributes:)];
  19. });
  20. }
  21. //=================================================================
  22. // initWithString:
  23. //=================================================================
  24. #pragma mark - initWithString:
  25. - (instancetype)avoidCrashInitWithString:(NSString *)str {
  26. id object = nil;
  27. @try {
  28. object = [self avoidCrashInitWithString:str];
  29. }
  30. @catch (NSException *exception) {
  31. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  32. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  33. }
  34. @finally {
  35. return object;
  36. }
  37. }
  38. //=================================================================
  39. // initWithString:attributes:
  40. //=================================================================
  41. #pragma mark - initWithString:attributes:
  42. - (instancetype)avoidCrashInitWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs {
  43. id object = nil;
  44. @try {
  45. object = [self avoidCrashInitWithString:str attributes:attrs];
  46. }
  47. @catch (NSException *exception) {
  48. NSString *defaultToDo = AvoidCrashDefaultReturnNil;
  49. [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
  50. }
  51. @finally {
  52. return object;
  53. }
  54. }
  55. @end