{"id":367,"date":"2011-10-11T06:51:45","date_gmt":"2011-10-11T06:51:45","guid":{"rendered":"http:\/\/www.blogs.abeazam.com\/dev\/?p=367"},"modified":"2012-05-07T16:19:50","modified_gmt":"2012-05-07T16:19:50","slug":"the-description-method","status":"publish","type":"post","link":"http:\/\/www.blogs.abeazam.com\/dev\/2011\/10\/the-description-method\/","title":{"rendered":"The description method"},"content":{"rendered":"<p>Any object can\u00a0implement\u00a0the description method that is used to return a description of the\u00a0contents\u00a0of a\u00a0receiver. The problem i have always found with this method. Is keeping the method up to date during the\u00a0development\u00a0process. Also getting other\u00a0developers\u00a0working on the project also keeping it up to date.<\/p>\n<p>With that problem in mind I put together this method that will\u00a0automatically\u00a0generate\u00a0the string and list all\u00a0available\u00a0properties in the the\u00a0receiver. It will print out when the instance is used in NSLog or or printed using the GDB debugger commands in my\u00a0<a href=\"http:\/\/www.blogs.abeazam.com\/dev\/?p=339\">previous\u00a0post<\/a>.<\/p>\n<p>[objc]<br \/>\n#import &lt;objc\/runtime.h&gt;<\/p>\n<p>\/\/this will automatically create a description<br \/>\n\/\/based on the properties<br \/>\n&#8211; (NSString*) description<br \/>\n{<br \/>\n    unsigned int propCount, i;<br \/>\n    \/\/gets a list of all properties in the<br \/>\n    \/\/created in this class and the property count<br \/>\n    objc_property_t *properties = class_copyPropertyList([self class], &amp;propCount);<\/p>\n<p>    \/\/used to hold all the properties<br \/>\n    NSMutableArray *keys = [NSMutableArray arrayWithCapacity:propCount];<\/p>\n<p>    for(i = 0; i &lt; propCount; i++)<br \/>\n\t{<br \/>\n        \/\/extracts a single property<br \/>\n        objc_property_t property = properties[i];<br \/>\n        \/\/gets the property name<br \/>\n        const char *propName = property_getName(property);<\/p>\n<p>        \/\/checks to make sure there is a name<br \/>\n        if(propName)<br \/>\n\t\t{<br \/>\n            \/\/converts the name to a usable object<br \/>\n            NSString *propertyName = [NSString stringWithCString:propName<br \/>\n                                                        encoding:NSNonLossyASCIIStringEncoding];<br \/>\n            \/\/adds it to a list of keys<br \/>\n            [keys addObject:propertyName];<br \/>\n        }<br \/>\n    }<br \/>\n    \/\/frees the properties<br \/>\n    free(properties);<\/p>\n<p>    \/\/attempt to simulate the dictionary log by using the %p to get the memory address<br \/>\n    \/\/the last %@ is used for all the items added to the dictionary<br \/>\n    return [NSString stringWithFormat:@&quot;&lt;%@: %p; %@&gt;&quot;,<br \/>\n                                      NSStringFromClass([self class]),<br \/>\n                                      self,<br \/>\n                                      [self dictionaryWithValuesForKeys:keys]];<br \/>\n}<br \/>\n[\/objc]<\/p>\n<p>You can also find it on my custom xCode 4 templates <a href=\"https:\/\/github.com\/abeazam\/xCode_4_File_Templates\/blob\/master\/Templates\/Custom%20iPhone\/NSObject.xctemplate\/NSObject\/___FILEBASENAME___.m\">here<\/a> on github. Right at the bottom the class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Any object can\u00a0implement\u00a0the description method that is used to return a description of the\u00a0contents\u00a0of a\u00a0receiver. The problem i have always found with this method. Is keeping the method up to date during the\u00a0development\u00a0process. Also getting other\u00a0developers\u00a0working on the project also keeping it up to date. With that problem in mind I put together this method [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[25,26,28,36,37,53],"tags":[],"_links":{"self":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/367"}],"collection":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":1,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":399,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/posts\/367\/revisions\/399"}],"wp:attachment":[{"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.blogs.abeazam.com\/dev\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}