问题
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"
- The socket.io the app sends unencrypted characters.
- The app successfully receives all characters
- 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