我想在 NSDictionary
中保存特殊字符/德语/瑞典字符,并且必须将此数据发布到服务器,但是保存在字典中的数据会转换为控制台输出中的其他格式。我试图将此字符串保存为不同的类型转换,但没有得到。
由于 NSDictionary
的数据类型是通用的,并且在发送到 POST 时以修改后的格式发送,我想将此数据按原样保存在 NSDictionary
中, 这样它就可以以正确的格式发送到服务器并在服务器端可读
我的代码是
NSString *playerName = @"Lëÿlã Råd Sölvê"; // dummy player name
NSLog(@"playerName: %@",playerName);
NSDictionary *postParameters = @{@"playerName1": playerName,
@"playerName2": [NSString stringWithString:playerName],
@"playerName3": [NSString stringWithUTF8String:[playerName UTF8String]],
@"playerName4": [NSString stringWithCString:[playerName UTF8String] encoding:NSASCIIStringEncoding],
@"playerName5": [[NSString alloc] initWithString:playerName],
@"playerName6": [NSString stringWithFormat:@"%@",playerName]};
NSLog(@"postParameters: %@",postParameters);
输出是
playerName: Lëÿlã Råd Sölvê
postParameters: {
playerName1 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName2 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName3 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName4 = "L\U00c3\U00ab\U00c3\U00bfl\U00c3\U00a3 R\U00c3\U00a5d S\U00c3\U00b6lv\U00c3\U00aa";
playerName5 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName6 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
}
我怎样才能实现这个目标...
最佳答案
您的代码没有任何问题。
您看到的是 NSLog
和 description
方法的产物 - 前者调用后者以获得输出对象的文本表示。对于 NSString
,字符串使用 Unicode 显示。但是,对于 NSDictionary
,包含的字符串使用 Objective-C Unicode 字符转义序列显示,格式为 '\Uxxxx'
。
为了确保自己一切正常,您可以使用:
for (NSString *key in postParameters)
NSLog(@"%@ -> %@", key, postParameters[key]);
一切都应该显示正常(除了 playerName4
你自己弄乱了字符串)。
关于ios - 在 NSDictionary 中保存特殊字符/瑞典语/德语字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21573885/