What is the Objective-C equivalent for "toString()", for use with NSLog?

Objective CTostringNslog

Objective C Problem Overview


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().

Objective C Solutions


Solution 1 - Objective C

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];
}

Solution 2 - Objective C

Add this to the @implementation of your Photo class:

- (NSString *)description {
   return [NSString stringWithFormat:@"Photo: Name=%@ Author=%@",name,author];
}

Solution 3 - Objective C

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.

Solution 4 - Objective C

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.

Solution 5 - Objective C

This will output the available voices:

    NSLog((@"speechVoices:%", [[AVSpeechSynthesisVoice speechVoices] description] ));

Solution 6 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeorge ArmholdView Question on Stackoverflow
Solution 1 - Objective CzakovyryaView Answer on Stackoverflow
Solution 2 - Objective CgrahamparksView Answer on Stackoverflow
Solution 3 - Objective CteabotView Answer on Stackoverflow
Solution 4 - Objective CMaddTheSaneView Answer on Stackoverflow
Solution 5 - Objective CgrigbView Answer on Stackoverflow
Solution 6 - Objective CErkki Nokso-KoivistoView Answer on Stackoverflow