UTF8的愚蠢问题 [英] Silly problem with UTF8
问题描述
这是一个愚蠢的n00b问题,但我只是不理解而已.我确实相信我需要转换为NSString并利用"stringWithUTF8String:",但不完全了解如何在此代码中实现它,因此我会在NSLog中获得正确的输出.我一直在环顾四周,但仍然无法正常工作. plist是正确的.
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.
我的输出是:
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];
}
正如您的示例所示,
推荐答案
Objective-C字符串文字严格来说不是7位ASCII.但是,您应该避免使用7位ASCII以外的任何东西来保持一致性和兼容性.
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.
遵循的工作是简单地使用Unicode安全地加载字符串.
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东西是用于多语言界面的东西.将显示的文本与显示的按钮等分开.
Localized String stuff is for multi-lingual interface stuff. Separating the text shown from the buttons shown, and such.
这篇关于UTF8的愚蠢问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!