Reputation: 11745
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?
Upvotes: 3
Views: 3116
Reputation: 8329
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.
Upvotes: 2
Reputation: 45180
There is a way, but I can't check it at the moment:
NSString *decodedString = [NSString stringWithUTF8String:[[test description] cStringUsingEncoding:[NSString defaultCStringEncoding]]];
NSLog(@"%@",decodedString);
Upvotes: 0