123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- //
- // NSAttributedString+AvoidCrash.m
- // https://github.com/chenfanfang/AvoidCrash
- //
- // Created by mac on 16/10/15.
- // Copyright © 2016年 chenfanfang. All rights reserved.
- //
- #import "NSAttributedString+AvoidCrash.h"
- #import "AvoidCrash.h"
- @implementation NSAttributedString (AvoidCrash)
- + (void)avoidCrashExchangeMethod {
-
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
-
- Class NSConcreteAttributedString = NSClassFromString(@"NSConcreteAttributedString");
-
- //initWithString:
- [AvoidCrash exchangeInstanceMethod:NSConcreteAttributedString method1Sel:@selector(initWithString:) method2Sel:@selector(avoidCrashInitWithString:)];
-
- //initWithAttributedString
- [AvoidCrash exchangeInstanceMethod:NSConcreteAttributedString method1Sel:@selector(initWithAttributedString:) method2Sel:@selector(avoidCrashInitWithAttributedString:)];
-
- //initWithString:attributes:
- [AvoidCrash exchangeInstanceMethod:NSConcreteAttributedString method1Sel:@selector(initWithString:attributes:) method2Sel:@selector(avoidCrashInitWithString:attributes:)];
- });
- }
- //=================================================================
- // initWithString:
- //=================================================================
- #pragma mark - initWithString:
- - (instancetype)avoidCrashInitWithString:(NSString *)str {
- id object = nil;
-
- @try {
- object = [self avoidCrashInitWithString:str];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
- }
- //=================================================================
- // initWithAttributedString
- //=================================================================
- #pragma mark - initWithAttributedString
- - (instancetype)avoidCrashInitWithAttributedString:(NSAttributedString *)attrStr {
- id object = nil;
-
- @try {
- object = [self avoidCrashInitWithAttributedString:attrStr];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
- }
- //=================================================================
- // initWithString:attributes:
- //=================================================================
- #pragma mark - initWithString:attributes:
- - (instancetype)avoidCrashInitWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs {
- id object = nil;
-
- @try {
- object = [self avoidCrashInitWithString:str attributes:attrs];
- }
- @catch (NSException *exception) {
- NSString *defaultToDo = AvoidCrashDefaultReturnNil;
- [AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
- }
- @finally {
- return object;
- }
- }
- @end
|