How to convert string to unicode(UTF-8) string in Swift?

Dirder picture Dirder · May 7, 2016 · Viewed 61.5k times · Source

How to convert string to unicode(UTF-8) string in Swift?

In Objective I could write smth like that:

NSString *str = [[NSString alloc] initWithUTF8String:[strToDecode cStringUsingEncoding:NSUTF8StringEncoding]];

how to do smth similar in Swift?

Answer

Alessandro Ornano picture Alessandro Ornano · Feb 26, 2018

Swift 4:

let str = String(describing: strToDecode.cString(using: String.Encoding.utf8))