176

Is there a method that I can override in my custom classes so that when

      NSLog(@"%@", myObject) 

is called, it will print the fields (or whatever I deem important) of my object? I guess I'm looking for the Objective-C equivalent of Java's toString().

George Armhold
  • 30,824
  • 50
  • 153
  • 232

6 Answers6

258

It is the description instance method, declared as:

- (NSString *)description

Here's an example implementation (thanks to grahamparks):

- (NSString *)description {
   return [NSString stringWithFormat: @"Photo: Name=%@ Author=%@", name, author];
}
zakovyrya
  • 9,579
  • 6
  • 39
  • 28
  • 6
    Note if you're using CoreData, the `description` property [is reserved](http://stackoverflow.com/questions/4717519/why-cant-i-use-description-as-an-attribute-name-for-a-core-data-entity) ...and will provide useful debugging information! In that case you'll need to come up with your own unique method name. – Nuthatch Dec 07 '13 at 04:09
  • Is `debugDescription` also reserved? Although I think `DebugDescription` is supposed to be used by a debugger like LLDB. – MaddTheSane Oct 23 '14 at 01:09
37

Add this to the @implementation of your Photo class:

- (NSString *)description {
   return [NSString stringWithFormat:@"Photo: Name=%@ Author=%@",name,author];
}
grahamparks
  • 16,130
  • 5
  • 49
  • 43
25

You can override the description method of NSObject:

- (NSString *)description

On the subject of logging I recommend this blog post for better logging in Objective-C.

teabot
  • 15,358
  • 11
  • 64
  • 79
  • 4
    Isn't this a static method? I'd like this to operate on objects rather than the class. For example, if I have a "Photo" class, with fields "name" and "author", I'd like NSLog to print those fields as they are assigned in the object. – George Armhold Jul 09 '09 at 15:50
  • 2
    Yes - well spotted - I pressed the wrong key. I clearly should pay more attention when proof reading my answers. Thankfully someone had their eye on the ball :-) – teabot Jul 09 '09 at 15:55
13

There are two functions that you can use.

- (NSString*)description

This will be displayed when you put your object as, I.E. a parameter for NSLog. The other description function is:

- (NSString*)debugDescription

This will be called when you do po anInstanceOfYourClass in the debug command window. If your class doesn't have a debugDescription function, then just description will be called.

Note that the base class NSObject does have description implemented, but it is fairly bare-bones: it only displays the address of the object. This is why I recommend that you implement description in any class you want to get info out of, especially if you use the description method in your code. If you do use description in your code, I suggest you implement debugDescription as well, also making debugDescription more verbose.

MaddTheSane
  • 2,981
  • 24
  • 27
1

This will output the available voices:

    NSLog((@"speechVoices:%", [[AVSpeechSynthesisVoice speechVoices] description] ));
grigb
  • 1,151
  • 8
  • 14
0

I think comment from @Nuthatch of overriding "description" with CoreData (i.e classes inheriting NSManagedObject) should be emphasized

https://developer.apple.com/documentation/coredata/nsmanagedobject?language=objc

Avoid overriding description—if this method fires a fault during a debugging operation, the results may be unpredictable.

Erkki Nokso-Koivisto
  • 1,203
  • 15
  • 19