Iphone:编码问题

时间:2010-01-06 07:53:21

标签: iphone encoding

我正在打开一个包含语言数据并将其读入字典的文本文件。代码检索所有数据,但我不能正确编码。文本文件中的字符(例如“”)被翻译为“√Ç”而不是“å”。

我开始对这个问题感到非常恼火,如果有人可以帮助我,我真的很感激...

CODE:

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *path = [[NSBundle mainBundle] pathForResource:[defaults objectForKey:@"language"]  
                                                     ofType:@"properties"];

    NSMutableDictionary *languageDictionary = [[NSMutableDictionary alloc] init];

    char character[1024];
    FILE *languageData = fopen([path cStringUsingEncoding:NSISOLatin1StringEncoding], "r");
    if (languageData == NULL) NSLog([path stringByAppendingString:@" not found"]);
    else
    {
        //check if end of line flag is set, and end while if so
        while (!feof(languageData)) 
        {
            fgets(character, 1024, languageData);
            NSString *stringFromChar = [NSString stringWithCString:character length:strlen(character)];
            NSArray *stringFromCharArray = [stringFromChar componentsSeparatedByString:@" = "];

            if(2 == [stringFromCharArray count])
            {
                [languageDictionary setObject:[stringFromCharArray objectAtIndex:1] forKey:[stringFromCharArray objectAtIndex:0]];
            }

        }

        fclose(languageData);
    }

2 个答案:

答案 0 :(得分:1)

你不应该这样做:

        NSString *stringFromChar = [NSString stringWithCString:character encoding: NSUTF16StringEncoding];

否则你将字符串加载为8位字符......

答案 1 :(得分:1)

找出文件的编码是什么(XCode:cmd + i,您选择的Texteditor:另存为),然后尝试使用here所述的适当NSString方法/编码。