Unicode data from NSData to NSString

Posted by Jeff on Stack Overflow See other posts from Stack Overflow or by Jeff
Published on 2011-02-06T21:44:41Z Indexed on 2011/02/06 23:25 UTC
Read the original article Hit count: 254

Filed under:
|
|
|
|

So if I have NSData from an HTTP request, then I do something like this:

NSString *test = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

This will result in null if the data contains weird unicode data (title is from reddit):

{"title":"click..¦¦me..and..then¦¦________ ¦¦check¦¦_.your...¦¦.__...¦¦____ ¦¦....¦¦¦¦¦¦¦¦¦¦¦¦¦¦....¦¦____ ¦¦¦¦¦¦....¦¦¦¦¦¦....¦¦¦¦¦¦____ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦____ ....¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦______ ........¦¦..._recently....¦¦________ ....¦¦....viewed....links....¦¦_____"},

How would I convert the data to a string? Ideally, it would best if the string wasn't null so I could parse it as JSON, but even a lossy conversion is fine with me in these cases.

I'm not familiar with unicode (naive American I am), so any enlightenment about that would be a nice bonus :)

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios