在 Swift 中,我使用 Decoding HTML NSAttributedString,如下所示:
let encodedString = "Ph\xe1\xba\xa3i c\xc3\xb4ng nh\xe1\xba\xadn r\xe1\xba\xb1ng k\xe1\xbb\x83 t\xe1\xbb\xab l\xc3\xbac \xc3\xb4ng Th\xc4\x83ng\xc2\xa0l\xc3\xa0m b\xe1\xbb\x99 tr\xc6\xb0\xe1\xbb\x9fng"\nlet encodedData = encodedString.dataUsingEncoding(NSUTF8StringEncoding)\nlet attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]\nlet attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil) \nlet decodedString = attributedString.string\nprintln(decodedString)\nRun Code Online (Sandbox Code Playgroud)\n\n但结果是这样的:
\n\n\n\n\nPh\xc3\xa1\xc2\xba\xc2\xa3i c\xc3\x83\xc2\xb4ng nh\xc3\xa1\xc2\xba\xc2\xadn r\xc3\xa1\xc2\xba\xc2\xb1ng k\ xc3\xa1\xc2\xbb\xc6\x92 t\xc3\xa1\xc2\xbb\xc2\xab l\xc3\x83\xc2\xbac \xc3\x83\xc2\xb4ng Th\xc3\x84\xc6\x92ng \xc3\x82\xc2\xa0l\xc3\x83\xc2\xa0m b\xc3\xa1\xc2\xbb\xe2\x84\xa2\n tr\xc3\x86\xc2\xb0\xc3\xa1\xc2\xbb \xc5\xb8ng
\n
真实结果一定与encodedString
这个方法有什么问题吗?
\n您必须在文档选项中指定使用的字符编码:
\n\nlet encodedString = "Ph\xe1\xba\xa3i c\xc3\xb4ng nh\xe1\xba\xadn r\xe1\xba\xb1ng k\xe1\xbb\x83 t\xe1\xbb\xab l\xc3\xbac \xc3\xb4ng Th\xc4\x83ng l\xc3\xa0m b\xe1\xbb\x99 tr\xc6\xb0\xe1\xbb\x9fng"\nlet encodedData = encodedString.data(using: .utf8)!\n\nlet attributedOptions : [NSAttributedString.DocumentReadingOptionKey : Any ] = [\n .documentType: NSAttributedString.DocumentType.html,\n .characterEncoding: String.Encoding.utf8.rawValue ]\ndo {\n let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)\n let decodedString = attributedString.string\n print(decodedString)\n} catch {\n // error ...\n}\n\n// Output: Ph\xe1\xba\xa3i c\xc3\xb4ng nh\xe1\xba\xadn r\xe1\xba\xb1ng k\xe1\xbb\x83 t\xe1\xbb\xab l\xc3\xbac \xc3\xb4ng Th\xc4\x83ng l\xc3\xa0m b\xe1\xbb\x99 tr\xc6\xb0\xe1\xbb\x9fng\nRun Code Online (Sandbox Code Playgroud)\n\n(针对 Swift 4 进行了更新)
\n| 归档时间: |
|
| 查看次数: |
2389 次 |
| 最近记录: |