Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS NSLog error with Unicode characters

Can anyone tell me the cause of the discrepancy for the following results ?

completionHandler:^(NSArray *placemarks, NSError *error) {
    NSLog(@"\n placemarks%@", placemarks);

Resulting:

placemarks(
"\U039b\U03b5\U03c9\U03c6\U03cc\U03c1\U03bf\U03c2 \U039a\U03cd\U03c0\U03c1\U03bf\U03c5 163, 16451 \U0391\U03c1\U03b3\U03c5\U03c1\U03bf\U03cd\U03c0\U03bf\U03bb\U03b7\U03c2, \U0395\U03bb\U03bb\U03ac\U03c2 @ <+37.90085408,+23.75654648> +/- 100.00m, region (identifier <+37.90085408,+23.75654648> radius 141.62) <+37.90085408,+23.75654648> radius 141.62m"
)

and

for(id object in placemarks ) {NSLog(@"%@ \n", object);}

Resulting

2012-09-14 13:08:23.493 ΑΦΜ[1390:c07] Λεωφόρος Κύπρου 163, 16451 Αργυρούπολης, Ελλάς @ <+37.90085408,+23.75654648> +/- 100.00m, region (identifier <+37.90085408,+23.75654648> radius 141.62) <+37.90085408,+23.75654648> radius 141.62m 

Thank you

like image 844
Μήτσος Avatar asked Sep 14 '12 10:09

Μήτσος


2 Answers

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.

like image 186
deanWombourne Avatar answered Sep 27 '22 18:09

deanWombourne


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.

like image 40
Chuck Krutsinger Avatar answered Sep 27 '22 18:09

Chuck Krutsinger