Ios NSDictionary说明不返回utf8字符?
我有一个以utf8字符串为对象的NSDictionary。打印对象会按其应有的方式打印特殊字符 但是,当我使用Ios NSDictionary说明不返回utf8字符?,ios,nsdictionary,Ios,Nsdictionary,我有一个以utf8字符串为对象的NSDictionary。打印对象会按其应有的方式打印特殊字符 但是,当我使用description方法将字典转换为字符串时,utf8字符无法正确打印出来 NSDictionary *test = [NSDictionary dictionaryWithObject:@"Céline Dion" forKey:@"bla"]; NSLog(@"%@",[test objectForKey:@"bla"]); // prints fine NSLog(@"%@",t
description
方法将字典转换为字符串时,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?有一种方法,但目前无法检查:
NSString *decodedString = [NSString stringWithUTF8String:[[test description] cStringUsingEncoding:[NSString defaultCStringEncoding]]];
NSLog(@"%@",decodedString);
我不担心-description的作用,它只是为了调试
从技术上讲,您没有UTF-8字符串。您有字符串(Unicode)。您不知道NSString在内部使用什么,也不应该在意。如果您想要一个UTF-8字符串(比如当您传递给C API时),请使用-UTF8String。为什么需要
-description
?有那么重要吗?您所需要的一切都是-valueForKey
(或-objectForKey
),它工作正常。我想将整个请求打印到文本视图中-是的,这只是为了开发,但正确打印utf8字符会更美观。我想知道我是否做错了什么。记住,将对象转换为字符串调用-description
。你是说在NSLog中?这也是为了调试。