Xcode complaints about “multi-character character contant”‘s when I try to do the following:
static unichar accent characters[] = { 'ā', 'á', 'ă', 'à' };
How do you make an array of characters, when not all of them are ascii? The following works just fine
static unichar accent[] = { 'a', 'b', 'c' };
Workaround
The closest work around I have found is to convert the special characters into hex, ie this works:
static unichar accent characters[] = { 0x0100, 0x0101, 0x0102 };
It’s not that Objective-C doesn’t like it, it’s that C doesn’t. The constant
'c'is forcharwhich has 1 byte, notunicharwhich has 2 bytes. (see the note below for a bit more detail.)There’s no perfectly supported way to represent a
unicharconstant. You can usein a UTF-8-encoded source file to get the unicode C-string, or
in a UTF-8 encoded source file to get an
NSString. (This was not possible before 10.5. It’s OK for iPhone.)NSStringitself is conceptually encoding-neutral; but if you want, you can get the unicode character by using-characterAtIndex:.Finally two comments:
If you just want to remove accents from the string, you can just use the method like this, without writing the table yourself:
See the document of CFStringFold.
Localizable.stringsandNSLocalizedString. See here.Note:
For arcane historical reasons,
'a'is anintin C, see the discussions here. In C++, it’s achar. But it doesn’t change the fact that writing more than one byte inside'...'is implementation-defined and not recommended. For example, see ISO C Standard 6.4.4.10. However, it was common in classic Mac OS to write the four-letter code enclosed in single quotes, like'APPL'. But that’s another story…Another complication is that accented letters are not always represented by 1 byte; it depends on the encoding. In UTF-8, it’s not. In ISO-8859-1, it is. And
unicharshould be in UTF-16. Did you save your source code in UTF-16? I think the default of XCode is UTF-8. GCC might do some encoding conversion depending on the setup, too…