Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSObject description and debugDescription

I have seen mentioning of description and debugDescription for debugging purposes, but have yet seen precise explanations of when to use which and under what conditions they may produce different results.

NSObject's documentation also doesn't have anything on debugDescription. Question: When to use which, and under what condition, should/would their output be different?

like image 925
Boon Avatar asked Feb 21 '12 14:02

Boon


2 Answers

Technical Note TN2124

Note: print-object actually calls the debugDescription method of the specified object. NSObject implements this method by calling through to the description method. Thus, by default, an object's debug description is the same as its description. However, you can override debugDescription if you want to decouple these; many Cocoa objects do this.

If you have debugDescription implemented, printing the object in GDB will display its result. Knowing that description is used in UI (I know bindings do that), you may want to use this to print some additional information that user doesn't need to see.

like image 151
hamstergene Avatar answered Nov 09 '22 18:11

hamstergene


One addition to what have already been told.

If you want to improve the output while working with po in lldb you can override the debugDescription method. Just keep in mind that printing self (the object itself) will call description method. If for some reason <ClassName: objectAddress> is not good for you also override that method.

So my point here was to highlight that printing self will call description method, whereas po calls debugDescription which by default calls description. Giving this you can differentiate the results of that calls.

like image 38
Julian Avatar answered Nov 09 '22 17:11

Julian