title: Swift Conversion UTF16 to UTF8 and back
body: <p>I'm building an app in Swift an I'm using Backendless as my backend. Turns out their database is UTF8 and thus I can't save emojis without converting the String first.
  I can't seem to find the right way to make this conversion to UTF8. I tried this:</p>
  
  <pre><code>let encoding = processedText.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
  </code></pre>
  
  <p>But after this operation the emojis look like this:
  %F0%9F%99%84%F0%9F%98%80%F0%9F%98%92%F0%9F%98%89%F0%9F%98%B6%F0%9F%98%B6%F0%9F%98%80%F0%9F%99%81</p>
  
  <p>And I tried this:
      class func stringToUTF8String (string: String) -> String? {</p>
  
  <pre><code>    let encodedData = string.dataUsingEncoding(NSUTF8StringEncoding)!
      let attributedOptions = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType]
      do{
      let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
      return attributedString.string
      }catch _ {
      }
  
      return nil
  }
  </code></pre>
  
  <p>And the emojis look like this:
  ðŸ¤”ðŸ¤”ðŸ‘…ðŸ™ðŸ˜‚ðŸ˜­ðŸ˜ŽðŸ˜‰ðŸ˜…ðŸ˜‰</p>
  
  <p>Does anyone have any suggestions? Thanks</p>