Re: printing arrays
Re: printing arrays
- Subject: Re: printing arrays
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sat, 03 Nov 2012 12:18:14 +0700
On 3 Nov 2012, at 00:35, Kyle Sluder <email@hidden> wrote:
> On Fri, Nov 2, 2012, at 10:02 AM, Gerriet M. Denkmann wrote:
>> This code:
>>
>> NSString *key = @"กุญแจ";
>> NSString *value = @"คุณค่า";
>> NSArray *array = @[ key, value ];
>> NSLog(@" Two nice strings: %@ %@", key, value);
>> NSLog(@" Bad Array: %@", array);
>>
>> prints:
>>
>> 2012-...] Two nice strings: กุญแจ คุณค่า
>> 2012-...] Bad Array: (
>> "\U0e01\U0e38\U0e0d\U0e41\U0e08",
>> "\U0e04\U0e38\U0e13\U0e04\U0e48\U0e32"
>> )
>>
>> Bug or feature?
>> Is there a way to make the array print in a more readable way?
>> 10.8.2, Xcode 4.5.2
>>
>> Gerriet.
>>
>> P.S. Same problem with dictionaries.
>>
>> P.P.S
>> NSLog(@" Bad Array3: %s", [[array description] UTF8String]); has exactly
>> the same unreadable output.
>
> Hopefully you're not relying on NSLog or -description producing reliable
> information.
No, these NSLog()s are just for debugging.
> If this is just for debugging purposes, you could swizzle -[NSArray
> description] and -[NSDictionary description].
I tried a Category for NSArray like:
@implementation NSArray( ReadableDescription )
- (NSString *)description
{
NSMutableString *m = [ NSMutableString stringWithString: @"\n(\n" ];
for( id item in self )
{
[ m appendString:@"\t"];
[ m appendString: [item description] ];
[ m appendString:@"\n"];
};
[ m appendString:@")\n"];
return m;
}
@end
Now this:
NSLog(@" Bad Array: %@", array);
NSLog(@" Good Array with description: %@", [array description]);
prints:
2012-11-03 12:04:14.548 Writing[84446:303] Bad Array: (
"\U0e01\U0e38\U0e0d\U0e41\U0e08",
"\U0e04\U0e38\U0e13\U0e04\U0e48\U0e32"
)
2012-11-03 12:04:14.548 Writing[84446:303] Good Array with description:
(
กุญแจ
คุณค่า
)
Is there a way to make the first NSLog work?
I seem to remember that it calls something like debuggingDescription, which, if not overridden calls description.
I have no experience with swizzling.
> Either way, file a bug.
Will do.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden