3

I find that if I set UILabel's text to "ԅ", it crashes without further information. It didn't hit the "All Exception" breakpoint.

self.label.text = @"ԅԅ";

How do I get rid of this crash? Thank you in advance!

By the way, I found this problem due to an emoticon "ԅ(¯﹃¯ԅ)", which kind of describes what I'm feeling right now.

Hampotato
  • 267
  • 4
  • 18

2 Answers2

5

Do not pass directly a Unicode character to NSString initialisation, instead use the character's Unicode code-point representation. In case of this character, it'd be @"\u504".

marekful
  • 14,986
  • 6
  • 37
  • 59
  • Thanks @marekful! My next question is, how would I know there's an Unicode character in NSString? For example, if someone posted "ԅ(¯﹃¯ԅ)", what should I do to prevent this crash? – Hampotato Aug 25 '15 at 11:20
  • Good question. It's tricky, since Unicode characters may be multibyte. Is this answer any good for you: http://stackoverflow.com/a/9830070/1207049? – marekful Aug 25 '15 at 11:26
  • Unfortunately in this case most characters in this NSString are UTF-8 characters, only the "ԅ" is Unicode. If I use `dataUsingEncoding:NSUnicodeStringEncoding`, the whole String becomes messy... – Hampotato Aug 25 '15 at 11:33
  • I tried `NSData* u = [comment.content dataUsingEncoding:NSUTF16BigEndianStringEncoding]; self.commentLabel.text = [[NSString alloc] initWithData:u encoding:NSUTF16BigEndianStringEncoding];` It works with normal Strings, but also crashes when it comes to "ԅ"... I'm going to beg my colleague to screen these characters on the server side. Thanks a lot:) – Hampotato Aug 25 '15 at 11:41
  • What do you want to happen when a user posts something with Unicode characters? – marekful Aug 25 '15 at 11:46
  • I'd love to show this character normally. If that's hard to accomplish, I wish to delete this character and prevent the app from crash. – Hampotato Aug 25 '15 at 12:51
  • To make some points clear: 1. All chars displayed *are* Unicode. Unicode is a char to index code. For the char `ԅ` the index is U+0505. 2. All chars in RAM should be UTF-16 (0x505), on disk usually UTF-8 (0xD4 0x85). UTF is an Unicode index to word/byte stream code. (So maybe changing the char encoding would help. However using `\u` is better by far.) 3. Therefore all chars you get back from the system at runtime using `unichar` are UTF-16 encoded. But take care: Since there are more than 65536 code points in Unicode, some very special Unicode chars may be encoded with two UTF-16 words. – Amin Negm-Awad Aug 25 '15 at 12:52
  • I don't understand why this answer was accepted and why it has so many up votes. Why shouldn't you directly pass Unicode characters in a string literal? I do it all of the time and have never had a problem. Using the `\Uxxxx` escape sequence is simply a compiler feature. The compiler converts the escape sequence into the literal character. So at runtime there is no difference between using the actual character or the escape sequence. – rmaddy Aug 25 '15 at 14:30
  • Dear, for one, 3 votes is not "so many". As for "Why shouldn't you directly pass Unicode characters in a string literal?", because his app crashed when he did so? ... – marekful Aug 25 '15 at 14:41
  • @rmaddy I accepted this answer because it helped me figure out what caused this crash:) Actually I didn't pass Unicode characters in a string literal, I was trying to display what user posted, and one of them posted this Unicode character. I had asked the server side colleague to screen these characters for me, and it solved the problem. – Hampotato Aug 26 '15 at 02:35
  • @Hampotato So then your question wasn't related to the problem and this answer isn't related to your problem. The question was about Unicode characters in a string literal and this answer was about escaping those literals. But now you say the problem is something completely different and has to do with some runtime values. I'm glad your issue is solved but this question and your problem are not the same. – rmaddy Aug 26 '15 at 02:37
  • I've been using inline japanese characters with `NSString`/`UILabel` since 2009, never had this issue... – Nicolas Miari Jun 24 '16 at 01:44
0

try this:

NSData *strd = [text dataUsingEncoding:NSUTF16StringEncoding];
NSString *newStr = [[NSString alloc] initWithData:strd encoding:NSUTF16StringEncoding];
Jimi
  • 1,091
  • 1
  • 14
  • 19