How do I escape a Unicode character in my Objective-C source code? How do I escape a Unicode character in my Objective-C source code? objective-c objective-c

How do I escape a Unicode character in my Objective-C source code?


Example:

NSString *stuff = @"The Greek letter Beta looks like this: \u03b2, and the emoji for books looks like this: \U0001F4DA";


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

[string stringByAppendingFormat:@"%C", 0x2665];


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.