简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM