From 18f48b88825dac05b7c11c9d5d5c72ab699c31dd Mon Sep 17 00:00:00 2001 From: FlyKite Date: Tue, 6 Dec 2016 17:31:47 +0800 Subject: [PATCH] Fix bug. --- FKLogUnicode/FKLogUnicode.m | 27 +++++++++++++++++-- .../project.pbxproj | 2 ++ .../FKLogUnicode/FKLogUnicode.m | 27 +++++++++++++++++-- FKLogUnicodeDemo/FKLogUnicodeDemo/main.m | 2 ++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/FKLogUnicode/FKLogUnicode.m b/FKLogUnicode/FKLogUnicode.m index d211d55..05480d1 100644 --- a/FKLogUnicode/FKLogUnicode.m +++ b/FKLogUnicode/FKLogUnicode.m @@ -17,8 +17,9 @@ @implementation NSObject (ExchangeSelector) * 如:将description与new_description的指针交换之后,执行description方法实际上将会执行自己写的new_description方法 */ + (void)exchangeSelector:(SEL)oldSel andNewSelector:(SEL)newSel { - Method oldMethod = class_getInstanceMethod([self class], oldSel); - Method newMethod = class_getInstanceMethod([self class], newSel); + Class cls = [self class]; + Method oldMethod = class_getInstanceMethod(cls, oldSel); + Method newMethod = class_getInstanceMethod(cls, newSel); // 改变两个方法的具体指针指向 method_exchangeImplementations(oldMethod, newMethod); } @@ -74,6 +75,17 @@ + (void)load { // 该方法会在加载这个类的时候执行(APP启动时会加载,只执行一次) // 此处交换descriptionWithLocale:与自己写的my_descriptionWithLocale:的方法指针 [self exchangeSelector:@selector(descriptionWithLocale:) andNewSelector:@selector(my_descriptionWithLocale:)]; + if ([self instancesRespondToSelector:@selector(descriptionWithLocale:indent:)]) { + [self exchangeSelector:@selector(descriptionWithLocale:indent:) andNewSelector:@selector(my_descriptionWithLocale:indent:)]; + } +} + +- (NSString *)my_descriptionWithLocale:(id)locale indent:(NSUInteger)level { + NSString *desc = [self my_descriptionWithLocale:(id)locale indent:(NSUInteger)level]; + if (level == 0) { + desc = [self replaceUnicode:desc]; + } + return desc; } - (NSString *)my_descriptionWithLocale:(id)locale { @@ -88,6 +100,17 @@ + (void)load { // 该方法会在加载这个类的时候执行(APP启动时会加载,只执行一次) // 此处交换descriptionWithLocale:与自己写的my_descriptionWithLocale:的方法指针 [self exchangeSelector:@selector(descriptionWithLocale:) andNewSelector:@selector(my_descriptionWithLocale:)]; + if ([self instancesRespondToSelector:@selector(descriptionWithLocale:indent:)]) { + [self exchangeSelector:@selector(descriptionWithLocale:indent:) andNewSelector:@selector(my_descriptionWithLocale:indent:)]; + } +} + +- (NSString *)my_descriptionWithLocale:(id)locale indent:(NSUInteger)level { + NSString *desc = [self my_descriptionWithLocale:(id)locale indent:(NSUInteger)level]; + if (level == 0) { + desc = [self replaceUnicode:desc]; + } + return desc; } - (NSString *)my_descriptionWithLocale:(id)locale { diff --git a/FKLogUnicodeDemo/FKLogUnicodeDemo.xcodeproj/project.pbxproj b/FKLogUnicodeDemo/FKLogUnicodeDemo.xcodeproj/project.pbxproj index f1251dc..9f4ae88 100644 --- a/FKLogUnicodeDemo/FKLogUnicodeDemo.xcodeproj/project.pbxproj +++ b/FKLogUnicodeDemo/FKLogUnicodeDemo.xcodeproj/project.pbxproj @@ -219,6 +219,7 @@ A68708B61C325B2A007021A8 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + MACOSX_DEPLOYMENT_TARGET = 10.12; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Debug; @@ -226,6 +227,7 @@ A68708B71C325B2A007021A8 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + MACOSX_DEPLOYMENT_TARGET = 10.12; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Release; diff --git a/FKLogUnicodeDemo/FKLogUnicodeDemo/FKLogUnicode/FKLogUnicode.m b/FKLogUnicodeDemo/FKLogUnicodeDemo/FKLogUnicode/FKLogUnicode.m index d211d55..05480d1 100644 --- a/FKLogUnicodeDemo/FKLogUnicodeDemo/FKLogUnicode/FKLogUnicode.m +++ b/FKLogUnicodeDemo/FKLogUnicodeDemo/FKLogUnicode/FKLogUnicode.m @@ -17,8 +17,9 @@ @implementation NSObject (ExchangeSelector) * 如:将description与new_description的指针交换之后,执行description方法实际上将会执行自己写的new_description方法 */ + (void)exchangeSelector:(SEL)oldSel andNewSelector:(SEL)newSel { - Method oldMethod = class_getInstanceMethod([self class], oldSel); - Method newMethod = class_getInstanceMethod([self class], newSel); + Class cls = [self class]; + Method oldMethod = class_getInstanceMethod(cls, oldSel); + Method newMethod = class_getInstanceMethod(cls, newSel); // 改变两个方法的具体指针指向 method_exchangeImplementations(oldMethod, newMethod); } @@ -74,6 +75,17 @@ + (void)load { // 该方法会在加载这个类的时候执行(APP启动时会加载,只执行一次) // 此处交换descriptionWithLocale:与自己写的my_descriptionWithLocale:的方法指针 [self exchangeSelector:@selector(descriptionWithLocale:) andNewSelector:@selector(my_descriptionWithLocale:)]; + if ([self instancesRespondToSelector:@selector(descriptionWithLocale:indent:)]) { + [self exchangeSelector:@selector(descriptionWithLocale:indent:) andNewSelector:@selector(my_descriptionWithLocale:indent:)]; + } +} + +- (NSString *)my_descriptionWithLocale:(id)locale indent:(NSUInteger)level { + NSString *desc = [self my_descriptionWithLocale:(id)locale indent:(NSUInteger)level]; + if (level == 0) { + desc = [self replaceUnicode:desc]; + } + return desc; } - (NSString *)my_descriptionWithLocale:(id)locale { @@ -88,6 +100,17 @@ + (void)load { // 该方法会在加载这个类的时候执行(APP启动时会加载,只执行一次) // 此处交换descriptionWithLocale:与自己写的my_descriptionWithLocale:的方法指针 [self exchangeSelector:@selector(descriptionWithLocale:) andNewSelector:@selector(my_descriptionWithLocale:)]; + if ([self instancesRespondToSelector:@selector(descriptionWithLocale:indent:)]) { + [self exchangeSelector:@selector(descriptionWithLocale:indent:) andNewSelector:@selector(my_descriptionWithLocale:indent:)]; + } +} + +- (NSString *)my_descriptionWithLocale:(id)locale indent:(NSUInteger)level { + NSString *desc = [self my_descriptionWithLocale:(id)locale indent:(NSUInteger)level]; + if (level == 0) { + desc = [self replaceUnicode:desc]; + } + return desc; } - (NSString *)my_descriptionWithLocale:(id)locale { diff --git a/FKLogUnicodeDemo/FKLogUnicodeDemo/main.m b/FKLogUnicodeDemo/FKLogUnicodeDemo/main.m index c876bf0..835d955 100644 --- a/FKLogUnicodeDemo/FKLogUnicodeDemo/main.m +++ b/FKLogUnicodeDemo/FKLogUnicodeDemo/main.m @@ -17,6 +17,8 @@ int main(int argc, const char * argv[]) { @"作用" : @"让NSLog输出字典和数组的时候输出汉字而不是Unicode编码" }; NSLog(@"%@", dict); + dict = @{@"数组":@[@"哈哈", @"附近的时刻", @"范德萨"], @"字典2":@{@"数组3":@[@1, @2, @3], @"字符串":@"附近的开始", @"字典":@{@"数组5":@[@3, @2,@1]}}}; + NSLog(@"%@", dict); } return 0; }