NSDictionary描述不返回utf8字符?
我有一个NSDictionary与utf8string作为对象。 打印对象时,应打印特殊字符。
但是,当我使用description
方法将字典转换为string时,utf8字符无法正确打印出来。
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
如何在保留utf8字符的同时打印NSDictionary?
我不担心什么 – 描述,只是为了debugging。
从技术上讲,你没有UTF-8string。 你有string(这是Unicode)。 你不知道NSString在内部使用什么,你不应该在意。 如果你想要一个UTF-8string(就像传递给C API),使用-UTF8String。
有一种方法,但目前我无法检查:
NSString *decodedString = [NSString stringWithUTF8String:[[test description] cStringUsingEncoding:[NSString defaultCStringEncoding]]]; NSLog(@"%@",decodedString);