0

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 Answers 2

2

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];
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.