2016-02-16 97 views
0

我使用Google Maps API,並在國外一些地址搜索時,地址回來與嵌入這樣的Unicode字符:Unicode向UTF8斯威夫特

"Place du Panth\U00e9on", 
"75005 Paris" 

該實例中的Unicode字符是\ U00e9這是é

我遇到的麻煩是,如果我已經將這些數據保存在JSON文件中並嘗試讀取它,SwiftyJSON pukes。 SwiftyJSON不喜歡反斜槓字符'\'JSON是有效的,即使我可以讀它,它仍然不好,因爲我寧願顯示正確以及所有其他Unicode字符。

有沒有人有任何想法如何將所有的unicode字符轉換爲Swift中該字符的UTF8編碼?

我應該只寫一個函數來搜索所有的Unicode字符,然後轉換它們嗎?

回答

0

除非有人有一個更好的主意,我只是寫了這個功能,現在正在爲我做的伎倆。

func convertFromUnicode(var myString:String) -> String { 
    let convertDict:[String:String] = ["\\U00c0":"À", "\\U00c1" :"Á","\\U00c2":"Â","\\U00c3":"Ã","\\U00c4":"Ä","\\U00c5":"Å","\\U00c6":"Æ","\\U00c7":"Ç","\\U00c8":"È","\\U00c9":"É","\\U00ca":"Ê","\\U00cb":"Ë","\\U00cc":"Ì","\\U00cd":"Í","\\U00ce":"Î","\\U00cf":"Ï","\\U00d1":"Ñ","\\U00d2":"Ò","\\U00d3":"Ó","\\U00d4":"Ô","\\U00d5":"Õ","\\U00d6":"Ö","\\U00d8":"Ø","\\U00d9":"Ù","\\U00da":"Ú","\\U00db":"Û","\\U00dc":"Ü","\\U00dd":"Ý","\\U00df":"ß","\\U00e0":"à","\\U00e1":"á","\\U00e2":"â","\\U00e3":"ã","\\U00e4":"ä","\\U00e5":"å","\\U00e6":"æ","\\U00e7":"ç","\\U00e8":"è","\\U00e9":"é","\\U00ea":"ê","\\U00eb":"ë","\\U00ec":"ì","\\U00ed":"í","\\U00ee":"î","\\U00ef":"ï","\\U00f0":"ð","\\U00f1":"ñ","\\U00f2":"ò","\\U00f3":"ó","\\U00f4":"ô","\\U00f5":"õ","\\U00f6":"ö","\\U00f8":"ø","\\U00f9":"ù","\\U00fa":"ú","\\U00fb":"û","\\U00fc":"ü","\\U00fd":"ý","\\U00ff":"ÿ"] 

    for (key,value) in convertDict { 
     myString = myString.stringByReplacingOccurrencesOfString(key, withString: value) 

    } 
return myString 
}