Skip to content

Instantly share code, notes, and snippets.

@github-xiaogang
Last active November 2, 2015 07:02
Show Gist options
  • Save github-xiaogang/8cae1d83dff36c934861 to your computer and use it in GitHub Desktop.
Save github-xiaogang/8cae1d83dff36c934861 to your computer and use it in GitHub Desktop.
替换Xcode打印日志中的unicode为中文
#if DEBUG
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@implementation NSObject (DEBUGGING)
+ (void)replaceClassMethodWithClass: (Class)clazz originMethod: (SEL)originMethodSEL withMethod: (SEL)newMethodSEL
{
Method originMethod = class_getInstanceMethod(clazz, originMethodSEL);
Method newMethod = class_getInstanceMethod(clazz, newMethodSEL);
method_exchangeImplementations(originMethod, newMethod);
}
@end
@implementation NSString (DEBUGGING)
//http://stackoverflow.com/questions/2099349/using-objective-c-cocoa-to-unescape-unicode-characters-ie-u1234?lq=1
+ (NSString*) stringByReplaceUnicode:(NSString*)string
{
NSMutableString * convertedString = [[string mutableCopy] autorelease];
[convertedString replaceOccurrencesOfString:@"\\U" withString:@"\\u" options:0 range:NSMakeRange(0, convertedString.length)];
CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);
return convertedString;
}
@end
@implementation NSDictionary (DEBUGING)
+ (void)load
{
[self replaceClassMethodWithClass:[NSDictionary class] originMethod:@selector(description) withMethod:@selector(Exchangeddescription)];
[self replaceClassMethodWithClass:[NSDictionary class] originMethod:@selector(descriptionWithLocale:) withMethod:@selector(ExchangeddescriptionWithLocale:)];
[self replaceClassMethodWithClass:[NSDictionary class] originMethod:@selector(descriptionWithLocale:indent:) withMethod:@selector(ExchangeddescriptionWithLocale:indent:)];
}
- (NSString *)Exchangeddescription
{
NSString * description = [self Exchangeddescription];
description = [NSString stringByReplaceUnicode:description];
return description;
}
- (NSString *)ExchangeddescriptionWithLocale:(id)locale
{
NSString * description = [self ExchangeddescriptionWithLocale:locale];
description = [NSString stringByReplaceUnicode:description];
return description;
}
- (NSString *)ExchangeddescriptionWithLocale:(id)locale indent:(NSUInteger)level
{
NSString * description = [self ExchangeddescriptionWithLocale:locale indent:level];
description = [NSString stringByReplaceUnicode:description];
return description;
}
@end
@implementation NSArray (DEBUGGING)
+ (void)load
{
[self replaceClassMethodWithClass:[NSArray class] originMethod:@selector(description) withMethod:@selector(Exchangeddescription)];
[self replaceClassMethodWithClass:[NSArray class] originMethod:@selector(descriptionWithLocale:) withMethod:@selector(ExchangeddescriptionWithLocale:)];
[self replaceClassMethodWithClass:[NSArray class] originMethod:@selector(descriptionWithLocale:indent:) withMethod:@selector(ExchangeddescriptionWithLocale:indent:)];
}
- (NSString *)Exchangeddescription
{
NSString * description = [self Exchangeddescription];
description = [NSString stringByReplaceUnicode:description];
return description;
}
- (NSString *)ExchangeddescriptionWithLocale:(id)locale
{
NSString * description = [self ExchangeddescriptionWithLocale:locale];
description = [NSString stringByReplaceUnicode:description];
return description;
}
- (NSString *)ExchangeddescriptionWithLocale:(id)locale indent:(NSUInteger)level
{
NSString * description = [self ExchangeddescriptionWithLocale:locale indent:level];
description = [NSString stringByReplaceUnicode:description];
return description;
}
@end
@implementation NSSet (DEBUGGING)
+ (void)load
{
[self replaceClassMethodWithClass:[NSSet class] originMethod:@selector(description) withMethod:@selector(Exchangeddescription)];
[self replaceClassMethodWithClass:[NSSet class] originMethod:@selector(descriptionWithLocale:) withMethod:@selector(ExchangeddescriptionWithLocale:)];
}
- (NSString *)Exchangeddescription
{
NSString * description = [self Exchangeddescription];
description = [NSString stringByReplaceUnicode:description];
return description;
}
- (NSString *)ExchangeddescriptionWithLocale:(id)locale
{
NSString * description = [self ExchangeddescriptionWithLocale:locale];
description = [NSString stringByReplaceUnicode:description];
return description;
}
@end
#endif
@github-xiaogang
Copy link
Author

Before

unicode

After

default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment