Xcode控制臺(tái)中文問題

在開發(fā)中我們經(jīng)常需要在控制臺(tái)中打印出一些數(shù)據(jù),以驗(yàn)證我們代碼的正確性。一般我們的需求都是會(huì)打印出網(wǎng)絡(luò)請(qǐng)求的返回結(jié)果,返回的大部分都是json數(shù)據(jù),但是直接輸出json數(shù)據(jù)時(shí)中文總會(huì)以原始碼文顯示,而不是正常顯示中文。

head =  {
            "is_auth" = "1.0";
            "last_pack" = "1.0";
            message = "\U64cd\U4f5c\U6210\U529f";
         }

打印出的都是unicode編碼,非常不方便我們迅速的理解。此時(shí)其實(shí)打印的結(jié)果基本沒什么意義了。我們需要的是這樣

"head" : {
      "is_auth" : "1.0",
      "last_pack" : "1.0",
      "message" : "操作成功",
      }
解決辦法
  • 使用代碼
    直接將json數(shù)據(jù)或者字典轉(zhuǎn)換為NSData
// json數(shù)據(jù)或者NSDictionary轉(zhuǎn)為NSData,responseObject為json數(shù)據(jù)或者NSDictionary
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
// NSData轉(zhuǎn)為NSString
NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonStr = %@", jsonStr);
  • 重寫description方法,也就是為字典或者數(shù)組添加一個(gè)分類
    當(dāng)字典或者數(shù)組被打印的時(shí)候,系統(tǒng)自動(dòng)調(diào)用重寫的description方法不需要將該分類導(dǎo)入到任何類中。
    description方法有3個(gè)方法
    • descriptionWithLocale:indent:
    • descriptionWithLocale:
    • description
      這3個(gè)方法的調(diào)用順序如下,
      descriptionWithLocale:indent:-> descriptionWithLocale:
      -> description
      官方文檔中也說明了調(diào)用順序

The returned NSString object contains the string representations of each of the dictionary’s entries. descriptionWithLocale:indent: obtains the string representation of a given key or value as follows:
If the object is an NSString object, it is used as is.
If the object responds to descriptionWithLocale:indent:, that method is invoked to obtain the object’s string representation.
If the object responds to descriptionWithLocale:, that method is invoked to obtain the object’s string representation.
If none of the above conditions is met, the object’s string representation is obtained by through its description property.

分類代碼如下

#import "NSDictionary+Log.h"

@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale{
    
    NSMutableString *strM = [NSMutableString stringWithString:@"{\n"];
    
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [strM appendFormat:@"\t%@ = %@;\n", key, obj];
    }];
    
    [strM appendString:@"}\n"];
    
    return strM;
}

@end

@implementation NSArray (Log)

- (NSString *)descriptionWithLocale:(id)locale{
    NSMutableString *strM = [NSMutableString stringWithString:@"(\n"];
    
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [strM appendFormat:@"\t%@,\n", obj];
    }];
    
    [strM appendString:@")"];
    
    return strM;
}

@end
  • 使用第三方插件(不推薦)
    FKConsole是一個(gè)用于在Xcode控制臺(tái)顯示中文的插件。地址直達(dá)這里
    詳情請(qǐng)查看官方的文檔,這里只貼出效果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評(píng)論 19 139
  • 村子里總是有些狗,雖都不一樣但誰也不認(rèn)識(shí)是誰家的狗,所謂“打狗看主人”得到誰家的門口方知道看哪位主人。 狗大多是擔(dān)...
    一縷陽光yg閱讀 602評(píng)論 0 5
  • 人生下來的時(shí)候,是一團(tuán)火光。然后慢慢熄滅。 ——Mr.Nav 序言 月亮碩大無比,照出周圍層層的云,月亮懸在大昭寺...
    NAV辭典閱讀 620評(píng)論 0 3
  • 記得是假期剛開學(xué) 晚上的火車 回學(xué)校時(shí)差點(diǎn)就要關(guān)門 你去車站接我 你拉著行李 我拉著你的手 風(fēng)在吹,葉在搖 突然的...
    玲子lee閱讀 272評(píng)論 0 0

友情鏈接更多精彩內(nèi)容