textEncodingName转NSStringEncodin

2015-11-06  本文已影响0人  无聊的啊飞

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)
}
上一篇 下一篇

猜你喜欢

热点阅读