How do I avoid the Unicode character in Objective-C source code?

I feel incredibly stupid to ask this, but the docs and google give me no love.

I have a Unicode character that I want to insert into a string literal in the source code of my iPhone application. I know its hexadecimal value. What's the correct escape sequence to use? And for that matter, what obvious source of information am I missing, what would tell me about this?

+33


a source to share


5 answers


Example:



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

      

+56


a source


If you don't want to use it directly in your string, you can use the format specifier:



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

      

+10


a source


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 example, I mark my line log with emoji because their distinctive colors are an easier way to select them:

message: @

So, if you have a character, not just code, and you want to use it in a static string rather than dynamically generating it, this is a great approach, since you immediately know which character you are using by looking at it.

+3


a source


The correct escape sequence would be something like strings

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 for general C / C ++, but I have not worked with iPhone development, so your mileage may vary.

+1


a source


\

is an escape character in object c, use it before the letter is escaped like this:

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

      

Here if you print the temp line in text view or logs you will also see "

because we used before it \

which is the escape character

-3


a source







All Articles