2

I have old code:

   func htmlToText(encodedString:String) -> String?
{
    let encodedData = encodedString.dataUsingEncoding(NSUTF8StringEncoding)!
    do
    {
        return try NSAttributedString(data: encodedData, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil).string
    } catch let error as NSError {
        print(error.localizedDescription)
        return nil
    }
}

and I want to translate it to swift 3, now i have:

let encodedData = encodedString.data(using: String.Encoding.utf8)!
    do
   {
        return try NSAttributedString(data: encodedData, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:String.Encoding.utf8], documentAttributes: nil).string
    } catch let error as NSError {
        print(error.localizedDescription)
        return nil
    }

This code generate error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue unsignedIntegerValue]: unrecognized selector sent to instance 0x608000251d30' I have no idea what could went wrong. Can anybody help me?

1 Answer 1

2

String.Encoding is a Swift struct, which cannot be passed to Objective-C world. When Swift find such things in Any, it generates _SwiftValue which is completely useless in Objective-C.

Try this:

return try NSAttributedString(data: encodedData, options: [
    NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
    NSCharacterEncodingDocumentAttribute:String.Encoding.utf8.rawValue
], documentAttributes: nil).string
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.