0
votes

I am having trouble using a NSString with unicode characters like 'ç' and 'ñ'. An example as simple as this:

NSString *alphabet = @"abçd";
NSLog(@"alphabet is %@", alphabet);

outputs this on the console:

alphabet is abçd

How can I ensure that NSString is handling these unicode characters? I tried several different approaches by searching online and on the NSString class reference but none seem to work.

Thanks in advance.

2

2 Answers

2
votes

you can be sure that NSString handled them correctly by outputting to a file, and reading from that...

I would guess with ~100% certainty that the problem isn't with NSString but the text encoding expected by the console, or the text encoding of your source file... probably the former.

in Xcode's debugger i get the expected output:

alphabet is abçd

your Test:

NSString *alphabet = @"abçd";       
[alphabet writeToFile:[@"~/Desktop/charTest.txt" stringByExpandingTildeInPath] atomically:NO encoding:NSUTF8StringEncoding error:nil];
0
votes

You can try to initialize the NSString with UTF-8 encoding

NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

Code was taken from another answer — Convert UTF-8 encoded NSData to NSString