Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's not that Objective-C doesn't like it, it's that C doesn't. The constant <code>'c'</code> is for <code>char</code> which has 1 byte, not <code>unichar</code> which has 2 bytes. (see the note below for a bit more detail.) </p> <p>There's no perfectly supported way to represent a <code>unichar</code> constant. You can use </p> <pre><code>char* s="ü"; </code></pre> <p>in a UTF-8-encoded source file to get the unicode C-string, or </p> <pre><code>NSString* s=@"ü"; </code></pre> <p>in a UTF-8 encoded source file to get an <code>NSString</code>. (This was not possible before 10.5. It's OK for iPhone.)</p> <p><code>NSString</code> itself is conceptually encoding-neutral; but if you want, you can get the unicode character by using <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-characterAtIndex_" rel="nofollow noreferrer"><code>-characterAtIndex:</code></a>. </p> <p>Finally two comments:</p> <ul> <li><p>If you just want to remove accents from the string, you can just use the method like this, without writing the table yourself:</p> <pre><code>-(NSString*)stringWithoutAccentsFromString:(NSString*)s { if (!s) return nil; NSMutableString *result = [NSMutableString stringWithString:s]; CFStringFold((CFMutableStringRef)result, kCFCompareDiacriticInsensitive, NULL); return result; } </code></pre> <p>See the document of <a href="http://developer.apple.com/mac/library/documentation/CoreFoundation/Reference/CFMutableStringRef/Reference/reference.html#//apple_ref/doc/uid/20001504-CH201-SW4" rel="nofollow noreferrer">CFStringFold</a>.</p></li> <li>If you want unicode characters for localization/internationalization, you shouldn't embed the strings in the source code. Instead you should use <code>Localizable.strings</code> and <code>NSLocalizedString</code>. See <a href="http://developer.apple.com/mac/library/DOCUMENTATION/MacOSX/Conceptual/BPInternational/Articles/StringsFiles.html#//apple_ref/doc/uid/20000005" rel="nofollow noreferrer">here</a>. </li> </ul> <p>Note: For arcane historical reasons, <code>'a'</code> is an <code>int</code> in C, see the discussions <a href="https://stackoverflow.com/questions/433895/why-are-c-character-literals-ints-instead-of-chars">here</a>. In C++, it's a <code>char</code>. But it doesn't change the fact that writing more than one byte inside <code>'...'</code> is implementation-defined and not recommended. For example, see <a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1336.pdf" rel="nofollow noreferrer">ISO C Standard 6.4.4.10</a>. However, it was common in classic Mac OS to write the four-letter code enclosed in single quotes, like <code>'APPL'</code>. But that's another story...</p> <p>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 <code>unichar</code> should 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...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload