swift UTF8编码和非UTF8字符

时间:2015-01-08 21:38:31

标签: string swift utf8-decode

我有一些来自json文件的文本,在本文中我已经应用了utf8编码,但是这个编码器不识别非标准字符àèìòù并且它是大写字母,有一种方法来净化ma字符串? 在这里我的功能

func stringToUTF8String (stringaDaConvertire stringa: String) -> String {

let encodedData = stringa.dataUsingEncoding(NSUTF8StringEncoding)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!
//println(attributedString.string)
return attributedString.string

}

感谢的

1 个答案:

答案 0 :(得分:5)

我找到了解决方案, utf8采用8位表ascii,而utf16采用16位ascii表,解决方法很简单,通过修改我的功能

func stringToUTF16String (stringaDaConvertire stringa: String) -> String {

let encodedData = stringa.dataUsingEncoding(NSUTF16StringEncoding)!
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)!
//println(attributedString.string)
return attributedString.string
}

简单的广告功能