Unicode for UTF8 in Swift

I use the Maps API and when searching for some addresses in foreign countries, the address is returned with Unicode characters embedded as follows:

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

In this case, the Unicode character is \ U00e9, which is equal to é

I have a problem: SwiftyJSON pukes, if I saved this data in a JSON file and try to read it. SwiftyJSON doesn't like the backslash character "\" JSON is valid, and even if I could read it, it's still not as good as I would prefer it to display correctly, as well as all other Unicode characters.

Does anyone have any ideas on how to convert all Unicode characters to UTF8 encoding of that character in Swift?

Should I just write a function that searches for all Unicode characters and then converts them?

0
source share
1 answer

If someone does not have a better idea, I just wrote this function, which now does the trick for me.

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

All Articles