NSDictionary description not returning utf8 characters?

北战南征 提交于 2019-12-10 19:44:32

问题


I have an NSDictionary with utf8 strings as objects. Printing the objects prints the special characters as they should.

But utf8 characters do not get correctly printed out when I convert the dictionary to a string with the description method.

NSDictionary *test = [NSDictionary dictionaryWithObject:@"Céline Dion" forKey:@"bla"];
NSLog(@"%@",[test objectForKey:@"bla"]); // prints fine
NSLog(@"%@",test);                       // does not print fine, é is replaced by \U00e
NSLog(@"%@",[test description]);         // also does not print fine

How can I print the NSDictionary while preserving utf8 characters?


回答1:


I wouldn't worry about what -description does, it's just for debugging.

Technically, you don't have UTF-8 strings. You have strings (which are Unicode). You don't know what NSString uses internally, and you shouldn't care. If you want a UTF-8 string (like when you're passing to a C API), use -UTF8String.




回答2:


There is a way, but I can't check it at the moment:

NSString *decodedString = [NSString stringWithUTF8String:[[test description] cStringUsingEncoding:[NSString defaultCStringEncoding]]];
NSLog(@"%@",decodedString);


来源:https://stackoverflow.com/questions/7123984/nsdictionary-description-not-returning-utf8-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!