0

I want to insert a unicode symbol for pi, which is \u03c0 into a label and for it to display the symbol. I am loading this in from an array which was read from a txt file. For example if I have a txt file that contains "\u03c0":

string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]
array[i] = string;
label.text = array[i];

What am getting is "\u03c0" as an output in the textfield, but I want the symbol. What I am doing wrong?

Edit: it seems that my problems is with string encoding because I am reading in the array from a file. I was using NSUTF8StringEncoding. What should this be changed to to allow unicode?

Amendale
  • 317
  • 3
  • 19

1 Answers1

1

My guess is the contents of your file contains \\u03c0 rather than the actual character. If you have control of the file contents, paste in the actual character, not the sequence, because the editor will save it with the escaping "\". If you don't have control, i suggest writing code to detect this escaping, strip the preceding "\" and then use the result in your format.

atomkirk
  • 3,701
  • 27
  • 30
  • I agree that putting in the literal character rather than escaping is a good workaround/solution. But I highly doubt the XCode editor would save out an extra backslash like that - that would be very strange behavior. – Brad Peabody Aug 26 '13 at 01:47
  • If you test it, post your findings. Itd be interesting if im wrong. – atomkirk Aug 26 '13 at 01:55
  • Just tried, I can't reproduce the issue. I put in: NSString *s = @"\u2603"; in XCode and those match the raw bytes in the file, and upon running the XCode console output gives me: "test: ☃" - as expected. Maybe the behavior is different in different XCode versions? (Mine is 4.6.3) Oh well. – Brad Peabody Aug 26 '13 at 02:06
  • Oh i meant if you save it as a non code file, like a txt. Ive hard coded the sequence in code before and it definitely works. Makes me wonder if an editor would save text differently based on extension. Probably not a code editor, youre right, but im sure TextEdit for example would escape the sequence. – atomkirk Aug 26 '13 at 02:23
  • Yes as bgp said if you use NSString *s = @"\u2603"; it works fine. However I am reading from a file. Using unicode didnt work for me, the text just showed up as \u03c0. atmok's workaround of just putting the literal character into the text file worked. Thanks :) – Amendale Aug 26 '13 at 03:12
  • Gotcha, I'm tracking now. That all makes sense. – Brad Peabody Aug 26 '13 at 03:46