开发者

Silly problem with UTF8

this is a silly n00b question but i just don't get it to work. I do believe that i need to convert to NSString and leverage "stringWithUTF8String:" but do not understand exactly how to implement it in this code so i get the right output in the NSLog. I have been looking around but it still do not work. The plist is correct.

My output is:

2010-11-07 21:43:00.419 plist_test[2984:207] Förbered och skriv  
2010-11-07 21:43:00.425 plist_test[2984:207] KLART  
2010-11-07 21:43:00.425 plist_test[2984:207] LÄS IN PLIST  
2010-11-07 21:43:00.427 plist_test[2984:207] array2: (  
    ETT,    
    "TV\U00c5",  ========Here is the problem, should be "TVÅ"    
    TRE,  
    FYRA  
)

Here is the code i am using.

- (void)viewDidLoad {

NSLog(@"Förbered och skriv");

NSMutableArray *array = [[NSMutableArray alloc] init];  
[array addObject:@"ETT"];  
[array addObject:@"TVÅ"];  
[array addObject:@"TRE"];  
[array addObject:@"FYRA"];  

[array writeToFile:@"/Users/PeterK/Desktop/plisttest.plist" atomically: TRUE];


NSLog(@"KLART");


NSLog(@"开发者_开发问答LÄS IN PLIST");

NSMutableArray *array2 = [[NSMutableArray alloc] init];

array2 = [NSMutableArray arrayWithContentsOfFile:@"/Users/PeterK/Desktop/plisttest.plist"];

NSLog(@"array2: %@", array2); ====here is the output


[super viewDidLoad];
}


Apple String Programming Guide says objective c string literals in your code are strictly 7-bit ASCII. (edit: they're not strictly 7-bit ASCII, but anything other than 7-bit ASCII means they're per-module and not linked into your static data section, which could lead to memory bloat. in other words, unless it's a memory burden, go for it.)

You can load your strings from data via the NSLocalizedString resources method.


Objective-C String Literals are not strictly 7-bit ASCII, as your example has shown. However, you should avoid using anything but 7-bit ASCII for consistency and compatibility.

The follow work for simply loading strings using Unicode safely.

+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;

Localized String stuff is for multi-lingual interface stuff. Separating the text shown from the buttons shown, and such.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜