-3

Aam getting long text from server and that text contains character \U201a\U00c4\U00f2He-Must-Not-Be-Named\U201a\U00c4\U00f4.

When I display text in textView am getting some different characters... How do I get normal Text in objective c??? Please help me out with this

When I received data from server I use

 infoDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

and from that infoDictionary I get text like

locks his cousin Dudley in the snake\U201a\U00c4\U00f4s captivity just in the blink of an eye. Each wand has a magical core such as phoenix\U201a\U00c4\U00f4s hair or dragon heartstring, that performs all the magic. \n

And I assign this value to textView like

 detailsTextView.text = [infoDictionary objectForKey:@"DESCRIPTION"];

But in textView am getting some different characters..

Akshay Ambekar
  • 325
  • 1
  • 3
  • 14
  • can yiou show your code – Anbu.Karthik Aug 31 '16 at 07:44
  • These characters are `Emoji` not normal, and your data is not encoded, the answer will only work with properly encoded data. Are you creating these data from app? or is it coming from api directly? – iphonic Aug 31 '16 at 08:05
  • @iphonic coming from api directly – Akshay Ambekar Aug 31 '16 at 08:06
  • Then you need to encode it properly using php or any language used in server side encoding methods, and the answer from @Anbu.Karthik will work for you. – iphonic Aug 31 '16 at 08:07
  • is it possible to decode on app side? – Akshay Ambekar Aug 31 '16 at 08:08
  • 1
    No, you have to Decode in your app, but the characters are not encoded properly as they are incorrect.. I would suggest one more thing.. is copy the whole json data and use any online [JSONVIEWER](http://jsonviewer.stack.hu/) to check what characters are they, so you can match with your app.. – iphonic Aug 31 '16 at 08:17

2 Answers2

2

There are two possibilities, one more likely, one less likely.

The less likely one is that your server sends rubbish when it tries to translate its data into JSON.

The more likely one is that you are just frightening yourself, and there is nothing wrong. Something like \U201a\U00c4\U00f2He-Must-Not-Be-Named\U201a\U00c4\U00f4 is exactly how non-ASCII characters are encoded in UTF-8. For example, U201A is the Unicode character "Single Low-9 Quotation Mark". Use the character viewer in MacOS X to find out what the characters are if you are curious. If you use NSLog, you will also get the same strange characters. They should be displayed in your text view perfectly fine.

However, in your case, the sequence \U00c4\U00f2 or \U00c4\U00f4 seems to be highly unusual. This would seem to be a problem with the server code, or with the actual data that is stored. If you are given rubbish data, there's nothing you can do about it. It's also not created by one of the typical stupid mistakes on the server (storing MacRoman characters, or taking UTF-8 and assume the bytes are code points). The only thing you can do is to contact whoever is supplying this data.

Now there is something you can do. You can use the method stringByReplacingOccurencesOfString: to replace nonsense data with something sensible. I wouldn't expect the sequence \U201a\U00c4\U00f4s = ’ to ever appear in a string that I display. So figure out what string belongs there (say a quotation mark) and replace it. So get the description into an NSString, use stringByReplacingOccurencesOfString: and use the result. There may be more strange combinations than just this one.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
gnasher729
  • 51,477
  • 5
  • 75
  • 98
-1

stringWithUTF8String: takes const char* as an argument, so no "@" symbol in the front.

NSString *description = [infoDictionary objectForKey:@"DESCRIPTION"];
NSString *str = [NSString stringWithUTF8String:description.UTF8String];

 detailsTextView.text = str;

Show this str in your textview.

pkc456
  • 8,350
  • 38
  • 53
  • 109