在開發(fā)中携栋,我們調(diào)試接口時最多的就是用NSLog 或者是打斷點(diǎn)來po 數(shù)據(jù),然而NSLog 輸出的數(shù)據(jù)中咳秉,中文是UFT-8格式顯示的婉支,根本無法調(diào)試,所以為了解決這個問題澜建,我在網(wǎng)上找了一些大神的做法向挖,也嘗試了幾種方法,發(fā)現(xiàn)了一種比較好用的炕舵,今天就分享給大家何之。最終效果也很好
image.png
主要思路就是利用runtime 來替換掉系統(tǒng)的輸出方法,然后將utf-8 轉(zhuǎn)換成中文樣式咽筋。
第一步就是要分別創(chuàng)建NSArry 和 NSDictionary的categogry
image.png
然后在.m文件中復(fù)制如下代碼即可大功告成
代碼如下
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
zx_swizzleSelector([self class], @selector(descriptionWithLocale:indent:), @selector(zx_descriptionWithLocale:indent:));
});
}
- (NSString *)zx_descriptionWithLocale:(id)locale indent:(NSUInteger)level
{
return [self stringByReplaceUnicode:[self zx_descriptionWithLocale:locale indent:level]];
}
- (NSString *)stringByReplaceUnicode:(NSString *)unicodeString
{
NSMutableString *convertedString = [unicodeString mutableCopy];
[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;
}
static inline void zx_swizzleSelector(Class theClass, SEL originalSelector, SEL swizzledSelector)
{
Method originalMethod = class_getInstanceMethod(theClass, originalSelector);
Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector);
BOOL didAddMethod =
class_addMethod(theClass,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(theClass,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}