Search code examples
iosnsstringnsstringencoding

Using unicode characters on NSString


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.


Solution

  • 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];