Unicode characters in string - iphone

RanLearns picture RanLearns · Mar 14, 2011 · Viewed 10k times · Source

I am working on a math app and need to output exponents to the screen.

I've found that this code will work:

NSLog(@"x\u2070 x\u00B9 x\u00B2 x\u00B3 x\u2074 x\u2075 x\u2076 x\u2077 x\u2078 x\u2079");

it displays: x⁰ x¹ x² x³ x⁴ x⁵ x⁶ x⁷ x⁸ x⁹

This also works:

NSString *testString = @"8.33x10\u00B3";
NSLog(@"test string: %@", testString);

it displays: test string: 8.33x10³

Even setting it to a label displays correctly on the iPhone screen:

NSString *testString = @"8.33x10\u00B3";
Answer1Label.text = testString;

However, when I pull the string from a .plist that says "8.33x10\u00B3" and display it on the screen, it just shows up as "8.33x10\u00B3" instead of 8.33x10³

Is there an additional character I need to put in front of the \u00B3 to get it to recognize?

Thanks for your help!

Answer

occulus picture occulus · Mar 14, 2011

The \uXXXX is converted into unicode at compile time, so you wouldn't expect that to be magically converted by reading a .plist.

Try opening the the plist file in Xcode in "text mode" (right click your plist file, Open As -> Plan Text File), then edit the desired string to contain the special characters by using text of the form:

⁰

rather than the usual \u2070 you've been using in-code. Then if you save your plist, close it, and open it again by double clicking, you'll see the usual plist editor view and it will contain your special characters.

Alternatively, consider using OS X's character viewer (aka character palette) to input the text directly into the plist editor in Xcode. More info.