[英]how to print Chinese not unicode when print NSDictionary in swift
當我打印包含中文的NSDictionary時,Xcode控制台打印unicode,如何將unicode轉換為中文?
試試這個解決方案
extension Dictionary {
var json: String {
let errorJson = "JSON Format error "
do {
let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return String(bytes: jsonData, encoding: String.Encoding.utf8) ?? errorJson
} catch {
return errorJson
}
}
}
使用代碼
let dictionary = ["name":"ahmad","age":"22","gender":"male"]
print(dictionary.json)
記錄結果:
{
"name":"ahmad",
"age":"22",
"gender":"male"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.