iOS: How to display special characters in Swift [NSString]

自作多情 提交于 2019-12-25 02:57:36

问题


I am sending data via socket.io my app, which I want to embed the text of a parameter in a UILabel

Javascript server

socket.emit("app",{ac:"organización"})

iOS App

var ac = self.jsonOFsocket["ac"] as String
//ac === "organización"
self.label.text = ac
//self.label.text === "organizaci\U00f3n"
  1. The socket.io the app sends unencrypted characters.
  2. The app successfully receives all characters
  3. The app shows different symbols

I try to do this:

func utf(txt:String) -> NSString {
   var newTxt = NSString(format:txt, NSUTF8StringEncoding)
   newTxt.precomposedStringWithCanonicalMapping
   return newTxt
}

var ac = self.jsonOFsocket["ac"] as String
//ac === "organización"
self.label.text = uft(ac)
//self.label.text === "organizaciââ¥n"

This is important:

organizaciââ¥n

回答1:


This is the Objective-C way:

NSData *data = [MyString dataUsingEncoding:NSUTF8StringEncoding];
NSString *stringWithSpecialCharacters = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];

You'll have to store the proper string format on your server too and make sure it returns the right string via curl.



来源:https://stackoverflow.com/questions/29501940/ios-how-to-display-special-characters-in-swift-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!