3

I have a textview that can contain multiple words and emoji's. However, I've noticed that some emoji's can crash my app

How I'm currently handling emoji's:

let data: NSData = myTextView.dataUsingEncoding(NSNonLossyASCIIStringEncoding)!
let valueUnicode: NSString = NSString(data: data, encoding: NSUTF8StringEncoding)!

Sometimes I get this error: "Invalid hex digit in unicode escape sequence around character 273."

The reason why I'm encoding is because I'm using json to store the textview into my db and later on I'm pulling it back down from the db

Lorenzo
  • 3,293
  • 4
  • 29
  • 56
  • 2
    What are you trying to do with this code? Why not simply access the `text` property to get the string? – rmaddy Oct 05 '15 at 00:55
  • @zaph @DevTonio The OP should be able to convert `myTextView.text` to valid JSON using `NSJSONSerialization` without performing any `NSData` conversions – Aaron Brager Oct 05 '15 at 01:04
  • I'm currently using serialization: let jsonData:NSDictionary = (try! NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers )) as! NSDictionary –  Oct 05 '15 at 01:07
  • @DevTonio If you are sending data to your server, you should use `NSJSONSerialization.dataWithJSONObject(_:options:)` to create an `NSData` object from a dictionary or array – Aaron Brager Oct 05 '15 at 01:14
  • @zaph That is correct; I'm saying the OP's use of `JSONObjectWithData(_:options:)` is incorrect. – Aaron Brager Oct 05 '15 at 01:17
  • So I should use dataWithJSONObject and do I still convert the string to utf8? –  Oct 05 '15 at 01:20
  • 1
    No, just create a dictionary or array using your string, then pass that to `dataWithJSONObject(_:options:)`. This will create an NSData object you can send to your web service. – Aaron Brager Oct 05 '15 at 01:26
  • Note that the original question did not mention JSON, my fault for starting to answer an incomplete question. – zaph Oct 05 '15 at 01:32
  • @zaph my apologies as i didn't think json was the cause of my problem. Thank you zaph & Aaron for pointing me in the right direction. I'll let you know if this solves my problem –  Oct 05 '15 at 01:33
  • The answer by @Aaron is correct. – zaph Oct 05 '15 at 01:35
  • Just changed my code and it works! thanks again –  Oct 05 '15 at 02:18

0 Answers0