Willam Hill Willam Hill - 1 year ago 302
iOS Question

Universal character name error in NSString when using unicode character, <

I'd like to use the Unicode version of '<' in an NSString but the compiler produces the error:

"Character '<' cannot be specified by a universal character name"

when I use:

NSString *text = @"Some Text: \u003C";

'<' seems to be a special character, as well as "=" and a few others so what's a way to insert a literal '<' inside the string, without literally using
like so:
"some string <"

I don't have control over the string value itself and the above value as in-line is for demonstration purpose.

Answer Source

I don't believe that the compiler error was addressed.

In response to the errors:

"Character '<' cannot be specified by a universal character name"
"Universal character name refers to a control character"

it appears that you cannot use the \U000000xxliteral syntax for many 2 bytes ASCII characters, with the following exceptions:

  • \U00000024
  • \U00000040
  • \U00000060
  • \U000000A0 to \U000000FF

A simple workaround is to use [NSString stringWithFormat:@"%C", 0x000000xx]

Example with the '<' character:

NSString *text = [NSString stringWithFormat:@"Hello %C", 0x003C]";

See xcode UTF-8 literals for more options.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download