How to code a NSString with UTF-8 ?
30/03/05 08:50
The Xcode editor lets you define a file encoding as UTF8. You can then type in a line like:
NSString *foo = @"blah blah";
where blah blah have Unicode characters in them with encoded as UTF8.
But, when I ask this string for its characters I get garbage back, and length is not correct.
NSString *foo = @"blah blah";
where blah blah have Unicode characters in them with encoded as UTF8.
But, when I ask this string for its characters I get garbage back, and length is not correct.
One possible misconception people come away in these discussions is that you need to use NSLocalizedString() if you want NSStrings with non-ASCII characters.
NSLocalizedString and friends are absolutely great choice if you need localizable strings, that is, strings which will need to be translated to different languages. The string is read from a .strings file, which can be made per-language.
However, if all you want is an NSString with some non-ASCII chars in it, and it's not meant to be shown to the user (and hence doesn't need to be localized), then it's perfectly fine to create the string programmatically. One possibility here is:
NSString *s = [NSString stringWithFormat:@"Long %C dash", 0x2014];
You can also do (since xE2x80x94 is the 3-byte UTF-8 string for 0x2014):
NSString *s = [NSString stringWithUTF8String:"Long xe2x80x94 dash"];
but one thing that is not very safe is to include actual high-bit characters in your source code:
NSString *s = [NSString stringWithUTF8String:"Long — dash"]; // Not safe; you're at the mercy of any tools you use
and the following is not allowed:
NSString *s = @"Long — dash"; // Not allowed
NSLocalizedString and friends are absolutely great choice if you need localizable strings, that is, strings which will need to be translated to different languages. The string is read from a .strings file, which can be made per-language.
However, if all you want is an NSString with some non-ASCII chars in it, and it's not meant to be shown to the user (and hence doesn't need to be localized), then it's perfectly fine to create the string programmatically. One possibility here is:
NSString *s = [NSString stringWithFormat:@"Long %C dash", 0x2014];
You can also do (since xE2x80x94 is the 3-byte UTF-8 string for 0x2014):
NSString *s = [NSString stringWithUTF8String:"Long xe2x80x94 dash"];
but one thing that is not very safe is to include actual high-bit characters in your source code:
NSString *s = [NSString stringWithUTF8String:"Long — dash"]; // Not safe; you're at the mercy of any tools you use
and the following is not allowed:
NSString *s = @"Long — dash"; // Not allowed