I am trying to decode data from an HTML page to be readable but the urlContent String is nil even though data returned from the NSURlSession is non-nil.
My implementation:
var city = "London"
var url = NSURL(string: "https://www.google.com/#safe=off&q=weather+in+" + city.stringByReplacingOccurrencesOfString(" ", withString: "+"))
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data , response, error) -> Void in
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding) as NSString!
println(urlContent)
})
datais an optionalNSData(or "NSData?"). You have to unwrap it.Update: I switched to
NSASCIIStringEncoding(fromNSUTF8StringEncoding) as Eric D. pointed out. I also updated the code for Swift 2.This works for me: