39

I feel incredibly stupid for asking this, but the documentation and Google are giving me no love at all.

I have a Unicode character I want to insert into a string literal in the source code of my iPhone app. I know its hex value. What is the proper escape sequence to use? And for that matter, what obvious source of information am I overlooking that would have told me this?

Becca Royal-Gordon
  • 17,541
  • 7
  • 56
  • 91
  • Similar question/answer for Swift: http://stackoverflow.com/questions/31284538/how-to-express-strings-in-swift-using-unicode-hexadecimal-values-utf-16 – Suragch Jul 09 '15 at 16:08

5 Answers5

67

Example:

NSString *stuff = @"The Greek letter Beta looks like this: \u03b2, and the emoji for books looks like this: \U0001F4DA";
Ky -
  • 30,724
  • 51
  • 192
  • 308
Alex
  • 2,438
  • 23
  • 30
  • 3
    You don't need to escape it. By default the editor and compiler toolchain will interpret the source code file as UTF8. You can use any unicode character in your string constants. This wasn't true previous to the Mac OS X 10.5 toolchain. – Ken Sep 06 '09 at 20:10
  • It also depends on what fonts you use. There are few fonts which doesn't support all unicodes. – Alex Markman Apr 25 '13 at 18:01
  • Note that this is only UTF-8. For all codepoints (e.g. emoji like U+1F638 that require UTF-16), @MarcCharbonneau's `%C`-based answer works (e.g. `...Format:@"%C", 0x1F638`). – Ky - Sep 21 '15 at 15:03
  • @Supuhstar UTF-8 could contain more than 2 bytes. And if it does, it is still called UTF-8. – Evgen Bodunov Jan 19 '16 at 18:32
  • @EvgenBodunov but how do you do it in a NSString literal? – Ky - Jan 19 '16 at 23:39
  • @Supuhstar please read wiki about UTF-8. Characters in UTF-8 could be different size, english letters - 1 byte, russian, for example, - 2 bytes, emoji - 3 bytes, etc. NSString works with UTF-8 strings natively. So you could write @"eng фя ", then save it as NSData and check size in bytes. – Evgen Bodunov Jan 21 '16 at 12:27
  • @EvgenBodunov yes, that's [_my_ answer](http://stackoverflow.com/a/32698729/453435) :P But you're saying Alex's answer of using a backslash-escape can work with high-page Unicode codepoints like U+1F638. How? – Ky - Jan 21 '16 at 12:32
  • 1
    @Supuhstar And i'm one of 3 upvoters on your answer. But you said that cat emoji requires UTF-16, and I disagree. It requires just longer escape sequence. There is two kinds of escape sequences short \uxxxx with two bytes, and long \Uxxxxxxxx - with four bytes. @"\U0001F638" longer shows cat. – Evgen Bodunov Jan 22 '16 at 14:24
11

If you don't want to put it directly in your string you can use a format specifier like this:

[string stringByAppendingFormat:@"%C", 0x2665];
Marc Charbonneau
  • 40,399
  • 3
  • 75
  • 82
4

A more modern approach:

I'm not sure when this feature was added to the language, but as of 2015, at least, Objective-C string literals can contain any Unicode character. For instance, I mark my log lines with emoji because their distinctive colors are an easier way for me to pick them out:

message:@" \n \n\t‼️ Error: %@"

So, if you have the character and not just the code point, and you want to use it in a static string and not dynamically generate it, this is a great approach, since you instantly know what character you're using just by looking at it.

Ky -
  • 30,724
  • 51
  • 192
  • 308
1

The proper escape sequence would be something along the lines of

wchar_t * str = L"\x0627";

See this question: character constant:\000 \xhh

Edit: Oh, sorry, I missed the iPhone and Objective-C tags. The above is valid for generic C/C++, but I have not worked with iPhone development so your mileage may vary.

Community
  • 1
  • 1
Christoffer
  • 12,712
  • 7
  • 37
  • 53
  • 1
    It works : "\u0024" for UTF-16 and "\x24" for UTF-8 in Objective-C, respectively "\u{0024}" and "\u{24}" in Swift ; my response doesn't concern about wide-chars – tontonCD Mar 21 '17 at 17:32
  • Yay, also works for unichar and character literals, e g `unichar x = L'•';`. (nice if you're working with NSString and getCharacters/stringWithCharacters) – nevyn Aug 31 '17 at 23:26
-4

\ is the escape character in objective c, use it before the letter to be escaped like :

NSString temp = @" hello \"temporary.h\" has been inported";

Here if you print the temp string in textview or logs, you will see " being printed as well because we have used the \ before them which is the escape character

blo0p3r
  • 6,790
  • 8
  • 49
  • 68
Khay
  • 981
  • 1
  • 10
  • 22
  • This does not answer the question, as it does not explain how the `\` escape character allows you to insert a Unicode character. – Ky - Sep 21 '15 at 15:05