UTF8 的愚蠢问题

发布于 2024-09-30 16:35:57 字数 1148 浏览 2 评论 0 原文

这是一个愚蠢的问题,但我就是无法让它发挥作用。我确实相信我需要转换为 NSString 并利用“stringWithUTF8String:”,但不完全理解如何在此代码中实现它,因此我在 NSLog 中获得正确的输出。我一直在环顾四周,但仍然不起作用。 plist 是正确的。

我的输出是:

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  
)

这是我正在使用的代码。

- (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];
}

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别在捏我脸啦 2024-10-07 16:35:57

Apple 字符串编程指南 说代码中的目标 C 字符串文字严格是 7 位 ASCII。
(编辑:它们不是严格的 7 位 ASCII,但是 7 位 ASCII 以外的任何内容都意味着它们是每个模块的,并且不会链接到静态数据部分,这可能会导致内存膨胀。换句话说,除非它是内存负担,去吧。)

您可以通过 NSLocalizedString 资源 方法。

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.

花海 2024-10-07 16:35:57

正如您的示例所示,Objective-C 字符串文字并不是严格的 7 位 ASCII。但是,为了一致性和兼容性,您应该避免使用除 7 位 ASCII 之外的任何内容。

以下工作用于简单地使用 Unicode 安全地加载字符串。

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

本地化字符串内容适用于多语言界面内容。将显示的文本与显示的按钮分开,等等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文