時常用Xcode除錯的工程師都知道,Xcode對中文字還不是很友善,因為除錯時印出的內容,中文字都會變成以\U為開頭的編碼,如:
\U900d\U9059\U6587\U5de5\U4f5c\U5ba4
儘管知道這是中文字,但實在很難確認這行代表什麼字啊⋯⋯
好在總是有解決方案,我強者同事阿宅,找到解法更發揚光大,讓任何人只要引用自訂Category,之後在Xcode除錯平台上必能如實顯示中文字!
一般而言我們常用容器Array與Dictionary,所以自訂這兩個Category匯入專案中,也不用import即能作用於所有專案中任何地方,實在太方便啦!
現在我就來分享這兩個Category(四個檔案),希望對所有以Xcode來寫程式的人都有注意,所謂「工欲善其事,必先利其器」,以增進我們工作效率與產能!
- NSArray+Log.h
#import <Foundation/Foundation.h> @interface NSArray (Log) @end
- NSArray+Log.m
#import "NSArray+Log.h" @implementation NSArray (Log) - (NSString *)descriptionWithLocale:(id)locale { NSMutableString *str = [NSMutableString stringWithFormat:@"%@ count:%lu \n[\n", [[self class] description], (unsigned long)self.count]; for (NSInteger k = 0; k < self.count; k++) { if (k != 0) { [str appendString:@",\n"]; } { id obj = self[k]; NSString *willAdd = [NSString stringWithFormat:@"%@", obj]; willAdd = [willAdd stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"]; willAdd = [@"\t" stringByAppendingString:willAdd]; [str appendString:willAdd]; } } [str appendString:@"\n]"]; return str; } @end
- NSDictionary+Log.h
#import <Foundation/Foundation.h> @interface NSDictionary (Log) @end
- NSDictionary+Log.m
#import "NSDictionary+Log.h" @implementation NSDictionary (Log) - (NSString *)descriptionWithLocale:(id)locale { NSArray *allKeys = [self allKeys]; NSMutableString *str = [NSMutableString stringWithFormat:@"%@ count:%lu \n{\n", [[self class] description], (unsigned long)self.count]; for (NSInteger k = 0; k < self.count; k++) { if (k != 0) { [str appendString:@",\n"]; } { id obj = allKeys[k]; NSString *willAdd = [NSString stringWithFormat:@"\"%@\" = ", obj]; willAdd = [@"\t" stringByAppendingString:willAdd]; [str appendString:willAdd]; } { NSString *key = allKeys[k]; id obj = self[key]; NSString *willAdd = [NSString stringWithFormat:@"%@", obj]; willAdd = [willAdd stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t\t"]; [str appendString:willAdd]; } } [str appendString:@"\n}"]; return str; } @end
現在我們就來測試套用前與套用後的效果,以下列程式碼為例:
NSDictionary *happyDict = @{@"Name" : @"黑皮棉", @"Company" : @"逍遙文工作室"}; NSLog(@"happyDict: %@", happyDict); NSArray *happyArr = @[@"黑皮棉", @"逍遙文工作室"]; NSLog(@"happyArr: %@", happyArr);
套用前:
happyDict: {
Company = “\U900d\U9059\U6587\U5de5\U4f5c\U5ba4″;
Name = “\U9ed1\U76ae\U68c9″;
}
happyArr: (
“\U9ed1\U76ae\U68c9″,
“\U900d\U9059\U6587\U5de5\U4f5c\U5ba4″
)
套用後:
happyDict: __NSDictionaryI count:2
{
“Company" = 逍遙文工作室,
“Name" = 黑皮棉
}
happyArr: __NSArrayI count:2
[
黑皮棉,
逍遙文工作室
]
是不是很讓人著迷呀!
隨意留個言吧:)~