NSString isEqualToString: not working

I use this code in my application. just found is incorrect when comparing Korean

        for (NSString *lang in array){
        NSString *currentLang = [[MLLanguage sharedInstance] lang];
        BOOL flag = [lang isEqualToString:currentLang];
        NSLog(@"\n'%@' isEqual to '%@', %d\n%@\n%@", lang, currentLang, flag?1:0, [lang dataUsingEncoding:NSUTF8StringEncoding], [currentLang dataUsingEncoding:NSUTF8StringEncoding]);

incorrect result: two Korean words are compared as different

        2012-06-19 21:16:52.681 Motilink[10188:11903] -[MLSettingLanguageViewController             loadDownloadedData][Line 50] 
        'English' isEqual to '한국어', 0
        <456e676c 697368>
        <ed959cea b5adec96 b4>
        2012-06-19 21:16:52.682 Motilink[10188:11903] -[MLSettingLanguageViewController             loadDownloadedData][Line 50] 
        '한국어' isEqual to '한국어', 0
        <e18492e1 85a1e186 abe18480 e185aee1 86a8e184 8be185a5>
        <ed959cea b5adec96 b4>
        2012-06-19 21:16:52.682 Motilink[10188:11903] -[MLSettingLanguageViewController             loadDownloadedData][Line 50] 
        '中国语' isEqual to '한국어', 0
        <e4b8ade5 9bbde8af ad>
        <ed959cea b5adec96 b4>

fix:

        2012-06-19 21:35:00.908 Motilink[10188:11903] -[MLSettingLanguageViewController loadDownloadedData][Line 50] 
        'English' isEqual to '中国语', 0
        <456e676c 697368>
        <e4b8ade5 9bbde8af ad>
        2012-06-19 21:35:00.909 Motilink[10188:11903] -[MLSettingLanguageViewController             loadDownloadedData][Line 50] 
        '한국어' isEqual to '中国语', 0
        <e18492e1 85a1e186 abe18480 e185aee1 86a8e184 8be185a5>
        <e4b8ade5 9bbde8af ad>
        2012-06-19 21:35:00.909 Motilink[10188:11903] -[MLSettingLanguageViewController loadDownloadedData][Line 50] 
        '中国语' isEqual to '中国语', 1
        <e4b8ade5 9bbde8af ad>
        <e4b8ade5 9bbde8af ad>

It seems that: NSString uses encoding on its own,

english only use 7 bytes like ascii

Usage in China uses 9 bytes, possibly utf8

but in Korean, it looks like two different results,

does anyone know this

+5
source share
1 answer

, . Unicode , . , "ä", "ä", "¨" "a".

: , ( UTF-8), - .

- , - [NSString precomposedStringWithCanonicalMapping]:

BOOL flag = [[lang precomposedStringWithCanonicalMapping] isEqualToString:
                    [currentLang precomposedStringWithCanonicalMapping]];
+8

All Articles