tags:

views:

63

answers:

3

I am curious how I might override the description method that is used when you do the following (see below) for an object. I basically want to better format the output, but am unsure about how I might go about setting this up.

NSLog(@"ARRAY: %@", myArray);

many thanks

EDIT_001

Although subclassing NSArray would have worked I instead decided that I would add a category to NSArray (having not used one before) Here is what I added ...

// ------------------------------------------------------------------- **
// CATAGORY: NSArray
// ------------------------------------------------------------------- **
@interface NSArray (displayNSArray)
    -(NSString*)display;
@end

@implementation NSArray (displayNSArray)
-(NSString*)display {
    id eachIndex;
    NSMutableString *outString = [[[NSMutableString alloc] init] autorelease];
    [outString appendString:@"("];
    for(eachIndex in self) {
        [outString appendString:[eachIndex description]];
        [outString appendString:@" "];
    }
    [outString insertString:@")" atIndex:[outString length]-1];
    return(outString);
}
@end

gary

+2  A: 

If you're doing this a lot, the easiest way to reformat the display of your array would be to add a new prettyPrint category to the NSArray class.

@interface NSArray ( PrettyPrintNSArray )
- (NSSTring *)prettyPrint;
@end

@implementation NSArray ( PrettyPrintNSArray )
- (NSString *)prettyPrint {
    NSMutableString *outputString = [[NSMutableString alloc] init];
    for( id item in self ) {
        [outputString appendString:[item description]];
    }
    return outputString;
}
@end

Obviously you'd need to alter the for loop to get the formatting the way you want it.

kubi
Thank you, the first line should be @implementation NSArray (PrettyPrintNSArray) ... Also @interface NSArray (PrettyPrintNSArray) -(NSString*)prettyPrint; @end will be needed before it too.
fuzzygoat
done. That's what I get for typing when 1/2 asleep.
kubi
+1  A: 

From Formatting string objects:

NSString supports the format characters defined for the ANSI C functionprintf(), plus ‘@’ for any object. If the object responds to the descriptionWithLocale: message, NSString sends that message to retrieve the text representation, otherwise, it sends a description message.

So to customize array conversion to string you should change NSArray descriptionWithLocale: implementation. Here's an example of how you can replace object method in run-time.

Vladimir
+2  A: 

I'm assuming that you myArray variable is an instance of the NSArray/NSMutableArray class.

When NSLog() encounters the @ character in its format string, it calls the -description: method on the object. This is a method on the root class, NSObject from which all other Cocoa classes inherit. -description: returns an NSString allowing any object that implements this method to be passed into NSLog(@"@",anyObject) and have a nicely formatted output. The string returned can be anything you care to construct.

For your specific problem, you could subclass NSMutableArray and override the -description: method with your own implementation. Then utilise your subclass instead of NSMutableArray.

For more information on NSObject and -description: see Apple's docs.

mikecsh