0

So I have this data being returned as nil with NSURLSession that had one field with an accented e. When the accented e was removed the data return works just fine.

Is there any way to have this work with the possibility of non-standard english characters being returned?

I am using the task format:

    let url = NSURL(string: "www.test.com")
    var session = NSURLSession.sharedSession()
    let loadDataTask = session.dataTaskWithURL(url!, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
        let string1 = NSString(data: data, encoding: NSUTF8StringEncoding)
        println(string1)
    }
Tamarisk
  • 929
  • 2
  • 11
  • 27
  • Have a look at [this answer](http://stackoverflow.com/a/32051684/2227743) it's a Swift version of what @dgatwood is talking about. – Eric Aya Sep 16 '15 at 08:47

1 Answers1

0

You're probably not getting the data back in UTF-8 encoding. I'm not sure how this translates into Swift, but here's what you'd do in Objective-C:

NSString *encodingName = response.textEncodingName;
CFStringEncoding cfsenc = CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)encodingName);
NSStringEncoding nssenc = CFStringConvertEncodingToNSStringEncoding(cfsenc);

Then pass that value instead of NSUTF8StringEncoding when you create the NSString.

Hope that helps.

dgatwood
  • 10,129
  • 1
  • 28
  • 49