Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

ios - Swift HTML Decoding Trouble

In Swift, I Decoding HTML using NSAttributedString, see below:

let encodedString = "Ph?i c?ng nh?n r?ng k? t? lúc ?ng Th?ng?làm b? tr??ng"
let encodedData = encodedString.dataUsingEncoding(NSUTF8StringEncoding)
let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
let attributedString = NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil, error: nil)         
let decodedString = attributedString.string
println(decodedString)

But the result like this:

Pháo£i c?′ng nháo-n ráo±ng ká?? tá?? l?oc ?′ng Th??ng??l??m bá?? tr?°á??ng

The true result must be the same with the encodedString

What's wrong in this method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to specify the used character encoding in the document options:

let encodedString = "Ph?i c?ng nh?n r?ng k? t? lúc ?ng Th?ng làm b? tr??ng"
let encodedData = encodedString.data(using: .utf8)!

let attributedOptions : [NSAttributedString.DocumentReadingOptionKey : Any ] = [
    .documentType: NSAttributedString.DocumentType.html,
    .characterEncoding: String.Encoding.utf8.rawValue ]
do {
    let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
    let decodedString = attributedString.string
    print(decodedString)
} catch {
    // error ...
}

// Output: Ph?i c?ng nh?n r?ng k? t? lúc ?ng Th?ng làm b? tr??ng

(Updated for Swift 4)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...