开发者

Iphone: encoding problem

开发者 https://www.devze.com 2022-12-15 18:57 出处:网络
I am opening a text file containing language data and reading it into a dictionary. The code retrieves all the data, but I can`t get the encoding right. Characters in the text file like for instance \

I am opening a text file containing language data and reading it into a dictionary. The code retrieves all the data, but I can`t get the encoding right. Characters in the text file like for instance "Â" gets translated to "√Ç" instead of "å".

I`m starting to get really annoyed by this problem, and i really appreciate if someone can help me out...

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"]开发者_Go百科);
    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);
    }


Shouldn't you be doing:

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

Otherwise you are loading up your strings as 8 bit chars...


Find out what the encoding of the file is (XCode: cmd+i, Texteditor of your choice: Save As) then try with appropriate NSString method/encoding described here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号