Nick Cartwright
Nick Cartwright

Reputation: 8274

Objective C override %@ for custom objects

I'd like to override the default print function in NSLog for custom objects;

For example:

MyObject *myObject = [[MyObject alloc] init];
NSLog(@"This is my object: %@", myObjcet);

Will print out:

This is my object: <MyObject: 0x4324234>

Is there a function I override in MyObject to print out a prettier description?

Cheers! Nick.

Upvotes: 19

Views: 4425

Answers (2)

Laurent Etiemble
Laurent Etiemble

Reputation: 27929

Just implement the description method.

- (NSString *)description {
     return @"MyCustomDescription";
}

This is the method used to print an instance.

Upvotes: 54

Williham Totland
Williham Totland

Reputation: 29039

Override -description in your custom class.

Upvotes: 8

Related Questions