Swift cannot read madarin from JSON api?

2020-05-05 18:24发布

问题:

I'm trying to read from the API's i've created using Kimono. However, when i print it in my console it seems like it can't read Mandarin Chinese in my xcode?

Am I missing something here?

Here's my code:

    let myContentsToBeScanned = NSURL(string: "https://www.kimonolabs.com/api/7ant5tkm?apikey=jlZUlWROWVJjPFrVbCwdmmcIaTWO6ISI")
    let myDataToBeRead = NSData(contentsOfURL: myContentsToBeScanned!)


    do {
        let myContentsFromJSON =  try NSJSONSerialization.JSONObjectWithData(myDataToBeRead!, options: [])
        print(myContentsFromJSON)
    } catch let err as NSError {
        print("nil")
    }

I am getting the output:

results =     {
    collection1 =         (
                    {
            articleSum = "\U611f\U53d7\U842c\U8056\U7bc0\U8a6d\U8b4e\U602a\U8a95\U53c8\U6b61\U6a02\U7121\U6bd4\U7684\U6c23\U6c1b\Uff0c\U5c31\U4f86 Garena \U96fb\U7af6\U9928\U5427\Uff01";
            articleTitles = "\U3010\U6d3b\U52d5\U3011\U5e7d\U6ba4\U65b0\U5a18\U904a\U8569\U4e2d\Uff0c\U842c\U8056\U7bc0\U4f86\U96fb\U7af6\U9928\U300c\U9b3c\U6df7\U300d\U5427\Uff01";

Thanks for your help

回答1:

It's simply that the default Xcode font, Menlo, does not support Chinese character. The fact that the Unicode code points are print means your characters are there.

You can verify this by opening the Font Book app, search Menlo, then Cmd + I to see the Font Info. Menlo supports the following languages:

Language: Afrikaans, Albanian, Azerbaijani, Basque, Belarusian, Bulgarian, Catalan, Cornish, Croatian, Czech, Danish, Dutch, English, Esperanto, Estonian, Faroese, Finnish, French, Galician, German, Greek, Hausa, Hawaiian, Hungarian, Icelandic, Indonesian, Irish, Italian, Kalaallisut, Kazakh, Latvian, Lithuanian, Macedonian, Malay, Maltese, Manx, Norwegian Bokmål, Norwegian Nynorsk, Oromo, Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovenian, Somali, Spanish, Swahili, Swedish, Swiss German, Turkish, Ukrainian, Uzbek, Welsh, Zulu

You can change Xcode to use a monospace font that does support Chinese, like MingLiU or SimHei. The easiest way to find out which font is available on your system is to create a Smart Collection:

  1. File > New Smart Collection
  2. Design Style = Monospaced
  3. Languages include Chinese