문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top