iOS NSLog error with Unicode characters

倖福魔咒の 提交于 2019-12-01 05:58:10

Interesting :)

Passing %@ into NSLog's format string just means 'call description on an object'.

It looks like description on NSArray deals with unicode characters differently than the description on each object.

However, I suspect that the description method on NSArray just calls description on each of the objects it contains and then, for some reason I'm not 100% sure about, is encoding them before dumping them out to NSLog.

Passing %@ to NSLog does call the [NSArray description] on the array. NSArray class reference says that [NSArray description] "returns a string that represents the contents of the array, formatted as a property list". Doing so converts unicode characters to NSNonLossyASCIIStringEncoding. Your NSArray will print correctly if you reverse the process.

completionHandler:^(NSArray *placemarks, NSError *error) {
       NSLog(@"%@", [NSString stringWithCString:[[placemarks description] cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSNonLossyASCIIStringEncoding]);

This works by converting the original string with all of the \Uxxxx encoded universal characters into a c string, then decoding the c string to an NSString reversing the NSNonLossyASCIIStringEncoding which had been done by the [NSLog description]. I have not figured out a way to do this without first converting to a c string.

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