0

I'm working on Ios and Swift 3 and I want to parse HTML Code. Until now Hpple did the trick for me using this code:

func parse_Html_Text(url:String)
{
    let data = NSData(contentsOf: URL(string: url)!)
    let doc = TFHpple(htmlData: data! as Data!)
}

But when I'm trying to get html code from a query url (like this one: link), then my app crashes and I get Thread1: EXC_BAD_INSTRUCTION error in this line:

let doc = TFHpple(htmlData: data! as Data!)

I also tried Alamofire's request method but I didnt manage to make it work

I'm stucked two days with this so any help will be appreciated.

Thank you in advance!

Elem J
  • 1

1 Answers1

0

I noticed that you use Swift 3. Probably the library expects an NSData object but I would suggest to write your function like the following:

func parse_Html_Text(url: String) {
    if let safeUrl = URL(string: url) {
        let data = try? Data(contentsOf: safeUrl)
        let doc = TFHpple(htmlData: data as? NSData)
    }
}

In this case keep in mind that we are not handling possible error from the data setter.

axel
  • 422
  • 2
  • 14
  • Thank you very much for your reply! The code give me error: Cannot convert value of type "NSData?" to expected argument type 'Data!' in line: let doc = TFHpple(htmlData: data as? NSData) Xcode suggest to add : as Data! at the end but , even thought it builds in that way, it returns empty doc and its unable to be parsed – Elem J Feb 11 '17 at 13:08