Skip to content

Commit

Permalink
Fix bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyKite committed Dec 6, 2016
1 parent dd3fbd5 commit 18f48b8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
27 changes: 25 additions & 2 deletions FKLogUnicode/FKLogUnicode.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions FKLogUnicodeDemo/FKLogUnicodeDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,15 @@
A68708B61C325B2A007021A8 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
MACOSX_DEPLOYMENT_TARGET = 10.12;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
A68708B71C325B2A007021A8 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
MACOSX_DEPLOYMENT_TARGET = 10.12;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand Down
27 changes: 25 additions & 2 deletions FKLogUnicodeDemo/FKLogUnicodeDemo/FKLogUnicode/FKLogUnicode.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions FKLogUnicodeDemo/FKLogUnicodeDemo/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 18f48b8

Please sign in to comment.