开发者

NSDictionary description not returning utf8 characters?

I have an NSDictionary with utf8 strings as objects. Prin开发者_StackOverflowting 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?


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.


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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜