How can I get a Swift / Xcode console to display Chinese characters instead of unicode?

I am using this code:

import SwiftHTTP var request = HTTPTask() var params = ["text": "θΏ™ζ˜―δΈ­ζ–‡ζ΅‹θ―•"] request.requestSerializer.headers["X-Mashape-Key"] = "jhzbBPIPLImsh26lfMU4Inpx7kUPp1lzNbijsncZYowlZdAfAD" request.responseSerializer = JSONResponseSerializer() request.POST("https://textanalysis.p.mashape.com/segmenter", parameters: params, success: {(response: HTTPResponse) in if let json: AnyObject = response.responseObject { println("\(json)") } },failure: {(error: NSError, response: HTTPResponse?) in println("\(error)") }) 

The Xcode console shows this as an answer:

 { result = "\U8fd9 \U662f \U4e2d\U6587 \U6d4b\U8bd5"; } 

Is it possible for the console to show the following ?:

 { result = "θΏ™ 是 δΈ­ζ–‡ εˆ†θ― ζ΅‹θ―•" } 

If so, what do I need to do to make this happen?

Thanks.

+6
source share
2 answers

Instead

 println(json) 

using

 println((json as NSDictionary)["result"]!) 

This will print the correct result in China.

Reason: The first print will cause a debug description for NSDictionary, which escapes not only Chinese characters.

+5
source

Your function actually prints the response object or, more precisely, its description. The response object is a dictionary, and Unicode characters are encoded using \ Uxxxx in their descriptions.

See question: NSJSONSerialization and Unicode won't mix well

To access the result line, you can do the following:

 if let json: AnyObject = response.responseObject { println("\(json)") // your initial println statement if let jsond = json as? Dictionary<String,AnyObject> { if let result = jsond["result"] as? String { println("result = \(result)") } } } 

The second println statement prints the actual line, and this code really gives:

 { result = "\U8fd9 \U662f \U4e2d\U6587 \U6d4b\U8bd5"; } result = θΏ™ 是 δΈ­ζ–‡ ζ΅‹θ―• 
+2
source

Source: https://habr.com/ru/post/978263/


All Articles