Objective-C
NSStringEncoding stringEncoding = NSUTF8StringEncoding;
NSString *textEncodingName = "utf-8";
CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)textEncodingName);
if (encoding != kCFStringEncodingInvalidId) {
stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding);
}
Swift
var stringEncoding = NSUTF8StringEncoding
let textEncodingName = "utf-8"
let encoding = CFStringConvertIANACharSetNameToEncoding(textEncodingName as CFString)
if encoding != kCFStringEncodingInvalidId {
stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding)
}