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()
}
3
  • 1
    Have a look at my answer here. You can let NSString guess the encoding. Commented Nov 19, 2014 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. Commented Nov 19, 2014 at 11:08
  • 1
    That's always the best solution (to be in control of the data) :) Commented Nov 19, 2014 at 11:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

thanks @user3280644. Knowing that the code produces valid results really help me shift my attention to the contents of the URL
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?

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.