title: SWIFT: NSURLSession convert data to String
body: <p>In my iPhone application (develops in SWIFT) I've got to communicate with a https service (with parameters) and needs to analyse the response.</p>
  
  <p>All works ok but in some cases noticed it does NOT getting the expected result... Further analysing I found it's the problem about converting server respone data to string (NSData -> NSString)...</p>
  
  <p>1). When I use <strong>UTF8</strong> Encoding I am getting nil as converted String (responseString )</p>
  
  <pre><code>    let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
  </code></pre>
  
  <p>2). But with <strong>ASCII</strong> encoding it's fine (Gets the correct response server provides)</p>
  
  <pre><code>    let responseString = NSString(data: data, encoding: NSASCIIStringEncoding)
  </code></pre>
  
  <p><strong>Following is a full sample code I am trying</strong>...</p>
  
  <pre><code>    let myUrl = NSURL(string: "https://myurl.com/myservice.asp")
      let request = NSMutableURLRequest(URL: myUrl!)
  
      request.HTTPMethod = "POST"
      request.timeoutInterval = 55.0
      let postString = "paramone=\(para1)&amp;paramtwo=\(para2)&amp;paramthree=\(para3)"
  
      // NOTE: Works ok with ASCII AND UTF8 both encoding types at this point...
      // request.HTTPBody = postString.dataUsingEncoding(NSASCIIStringEncoding)
      request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
  
      let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
  
          if (error != nil)
          {
              println("Error: \(error)")
              println("Description: \(error.description)")
              println("Domain     : \(error.domain)")
              println("Error code : \(error.code)")
          }
          else
          {
              //???? =&gt; ENCODING PROBLEM
              // let responseString = NSString(data: data, encoding: NSASCIIStringEncoding)
              let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
  
              println("Response: \(responseString)")
          }
      }
      task.resume()
  </code></pre>
  
  <p>I came across with few other <a href="http://iphonedevsdk.com/forum/iphone-sdk-development/50277-nsasciistringencoding-vs-nsutf8stringencoding.html" rel="nofollow">POSTS</a> explaining the same issue... But NOT sure if it's good to go with ASCII rather than UTF8... </p>
  
  <p>Also I can't understand the response contains '£' sign and works ok with ASCII encoding (eventhough '£' is NOT in ASCII character Set), BUT NOT with UTF8.</p>
  
  <p>Like to hear if I am missing anything or what the best way to go with this... Thanks. </p>