{"id":129,"date":"2008-09-16T19:50:33","date_gmt":"2008-09-17T00:50:33","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/09\/16\/description-little-known-hero-of-debugging\/"},"modified":"2008-09-16T19:50:36","modified_gmt":"2008-09-17T00:50:36","slug":"description-little-known-hero-of-debugging","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/09\/16\/description-little-known-hero-of-debugging\/","title":{"rendered":"-description, Little-Known Hero of Debugging"},"content":{"rendered":"<p><strong>Or: How to Make <code>NSLog()<\/code> Useful With Your Objects<\/strong><\/p>\n<p>Say you have an archaically named <code>NSArray<\/code> that you want to inspect &#8212; it&#8217;s easy to do, since <code>NSLog(@\"The bestiary is %@\", bestiary);<\/code> prints out the array&#8217;s contents<\/p>\n<blockquote><p>2008-09-16 19:46:06.445 Tester[2678:10b] The bestiary is (<br \/>\n    Cheetah,<br \/>\n    Pumpa,<br \/>\n    Jaguar,<br \/>\n    Panther,<br \/>\n    Tiger,<br \/>\n    Leopard<br \/>\n)\n<\/p><\/blockquote>\n<p>But if you try to <code>NSLog<\/code> your own object, you get pretty useless output, like <\/p>\n<blockquote><p>myObject = &lt;MyObject: 0x53f330&gt;<\/p><\/blockquote>\n<p>Fortunately, it&#8217;s easy to fix!  Just implement the method <a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/Foundation\/Protocols\/NSObject_Protocol\/Reference\/NSObject.html#\/\/apple_ref\/occ\/intfm\/NSObject\/description\"><code>-(NSString*) description;<\/code><\/a> and whatever it returns will be printed by <code>NSLog<\/code> and GDB (<code>po <em>object<\/em><\/code>, will print <em>object<\/em> in GDB and the Xcode debugging console).<\/p>\n<p>Here&#8217;s an (unfortunately complex) example,<br \/>\n<br \/><code>- (NSString*) description;<br \/>\n{<br \/>\n&nbsp;&nbsp;return [NSString stringWithFormat:@\"<%@ %p> = {\\n\\tquestion=%@,\\n\\tanswer=%@,\\n\\tsource=%@\\n}\", [self className], self, self.question, self.answer, self.source];<br \/>\n}<br \/><\/code><br \/>\noutput:<\/p>\n<blockquote><p>myObject = &lt;MyObject 0x53eed0&gt; = {<br \/>\n&nbsp;&nbsp;question=What is the Best Thing Ever Of All Times, Ever?,<br \/>\n&nbsp;&nbsp;answer=The Internet!,<br \/>\n&nbsp;&nbsp;source=http:\/\/www.cabel.name\/2008\/01\/2007-cabel-yay-awards.html<br \/>\n}<\/p><\/blockquote>\n<p><strong>Useful Formatters and such<\/strong><\/p>\n<p><a href=\"http:\/\/vgable.com\/blog\/2008\/08\/05\/simpler-logging-2\/\">These macros have made my debugging-life easer<\/a>.<\/p>\n<p><code>%p<\/code> tells <code>NSLog<\/code> to print the address of a pointer.<\/p>\n<p><code>-className<\/code> returns gives the name of a class as an <code>NSString<\/code>.<\/p>\n<p><em>Don&#8217;t manually print out a Cocoa <code>struct<\/code>, ever<\/em>, there are already <code>NSStringTo*<\/code> functions to do that for you, like <code>NSStringFromPoint()<\/code>.<\/p>\n<p><code>NSStringFromSelector()<\/code> works as advertized (and paired with <code>NSSelectorFromString()<\/code> is very useful in general).<\/p>\n<p><code>%lld<\/code> tells <code>NSLog<\/code> to print a <code>long long<\/code> (64-bit integer).  See also, <a href=\"http:\/\/www.cplusplus.com\/reference\/clibrary\/cstdio\/printf.html\"><code>printf<\/code> reference<\/a>.<\/p>\n<p><code>%Lf<\/code> tells <code>NSLog<\/code> to print a <code>long double<\/code>. See also, <a href=\"http:\/\/www.cplusplus.com\/reference\/clibrary\/cstdio\/printf.html\"><code>printf<\/code> reference<\/a>.<\/p>\n<p><strong>Best Practices<\/strong><\/p>\n<p>Whenever you make a new object, I <em>strongly<\/em> recommended immediately implementing a <code>description<\/code> method, and religiously keeping it up to date (it&#8217;s not hard, honest!).  This won&#8217;t fix bugs, but it will make finding some of them <em>much easier<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Or: How to Make NSLog() Useful With Your Objects Say you have an archaically named NSArray that you want to inspect &#8212; it&#8217;s easy to do, since NSLog(@&#8221;The bestiary is %@&#8221;, bestiary); prints out the array&#8217;s contents 2008-09-16 19:46:06.445 Tester[2678:10b] The bestiary is ( Cheetah, Pumpa, Jaguar, Panther, Tiger, Leopard ) But if you try [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,3,5,4,13,10],"tags":[77,83,174,78],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-macosx","category-objective-c","category-programming","category-sample-code","category-tips","tag-debugging","tag-nslog","tag-nsobject","tag-nsstring"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}