简体   繁体   中英

how to print Chinese not unicode when print NSDictionary in swift

当我打印包含中文的NSDictionary时,Xcode控制台打印unicode,如何将unicode转换为中文?

try this solution

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
        }
    }


}

using code

let dictionary = ["name":"ahmad","age":"22","gender":"male"]

print(dictionary.json)

result on log :

{
"name":"ahmad",
"age":"22",
"gender":"male"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM