19

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.

Nick Cartwright
  • 8,334
  • 15
  • 45
  • 56
  • Saw this note in my XCode 5.1 download details "Adds Quick Look support in the debugger for custom object types". Fingers crossed that it's all I am hoping from. Downloading now. – prototypical Mar 12 '14 at 21:40

2 Answers2

54

Just implement the description method.

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

This is the method used to print an instance.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
8

Override -description in your custom class.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68