3

Why would my code below successfully return with data, with a statusCode of 200 but fail to convert the returned NSData to an NSString?

var session: NSURLSession

func init() {
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    session = NSURLSession(configuration: config)
}

func getStatic(url:NSURL) {
    let request = NSMutableURLRequest(URL: url)
    let dataTask = session.dataTaskWithRequest(request) {(data, response, error) in
        if error != nil {
            // handle error
        } else {
            // data has a length of 2523 - the contents at the url
            if let httpRes = response as? NSHTTPURLResponse {
                 // httpRes is 200
                 let html = NSString(data:data, encoding:NSUTF8StringEncoding)
                 // **** html is nil ****
             }
        }
    }
    dataTask.resume()
}
Carl
  • 2,896
  • 2
  • 32
  • 50
  • 1
    Have a look at my answer [here](http://stackoverflow.com/questions/1351151/guess-encoding-when-creating-an-nsstring-from-nsdata/26740668#26740668). You can let NSString guess the encoding. – HAS Nov 19 '14 at 10:54
  • 1
    thanks @HAS. I didn't know about that new API. I'm in control of this data, in this example, but that API could be very useful in alternative cases. – Carl Nov 19 '14 at 11:08
  • 1
    That's always the best solution (to be in control of the data) :) – HAS Nov 19 '14 at 11:09

2 Answers2

4

The code is indeed correct.

The URL I was trying to load had non-UTF8 characters and so the encoding attempted by NSString(data:data, encoding:NSUTF8StringEncoding) failed.

Removing none UTF8 characters fixed the problem. Or selecting an appropriate encoding, NSISOLatin1StringEncoding for my content, also worked.

Carl
  • 2,896
  • 2
  • 32
  • 50
0

It looks like it should be fine, to me at least. I'm just dabbling in Swift, but I've done my enclosures (not sure if thats the right name) slightly changed like below. Did you try converting data to NSString prior to your if let httpRes = response as? NSHTTPURLResponse line? Maybe the data variable doesn't actually have the html in it. I have code written almost exactly the same with the changes below that I'm able to successfully convert data to a NSString.

let dataTask = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
    if error != nil {
        // handle error
    } else {
        if let httpRes = response as? NSHTTPURLResponse {
             let html = NSString(data:data, encoding:NSUTF8StringEncoding)
         }
    }
})

Hope it somehow helps.

syntheticgio
  • 588
  • 6
  • 25
  • thanks @user3280644. Knowing that the code produces valid results really help me shift my attention to the contents of the URL – Carl Nov 19 '14 at 10:39
  • 1
    The only real difference between your code and mine is the 'if let...' line. I'm having an unrelated problem with NSURLSession with the delegates not returning to the main function...maybe its just a little buggy overall? – syntheticgio Nov 19 '14 at 14:53