{"id":33,"date":"2010-04-26T22:21:11","date_gmt":"2010-04-27T06:21:11","guid":{"rendered":"http:\/\/bynomial.com\/blog\/?p=33"},"modified":"2010-08-06T17:35:29","modified_gmt":"2010-08-07T01:35:29","slug":"let-it-be","status":"publish","type":"post","link":"http:\/\/bynomial.com\/blog\/?p=33","title":{"rendered":"let it be"},"content":{"rendered":"<p>This post explains one of my favorite<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">NSObject<\/div><\/div>\n<p>categories &#8211; one that is extremely universal and handy for shorter and, in my opinion, more memory-manageable\u00c2\u00a0code. \u00c2\u00a0<em>(Code below.)<\/em><\/p>\n<p><strong><em>Edit<\/em><\/strong>: Commenter David found a bug in this implementation.  Check out <a href=\"http:\/\/bynomial.com\/blog\/?p=87\">the fixed version here<\/a>; the new source files are listed at the bottom of that post.  (The fix doesn&#8217;t require any change in usage or in the header file, just in the .m file).<\/p>\n<p>The idea is very simple &#8211; we can replace code that looks like this:<\/p>\n<pre>UIView *view = [[[UIView alloc] initWithFrame:myFrame] autorelease];\r\nMyObject *obj = [[[MyObject alloc] init] autorelease];<\/pre>\n<p>with this:<\/p>\n<pre>UIView *view = [[UIView be] initWithFrame:myFrame];\r\nMyObject *obj = [MyObject beInit];<\/pre>\n<p>As you can guess, we&#8217;re basically replacing the<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">alloc-autorelease<\/div><\/div>\n<p>pair with a single method,<\/p>\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">be<\/div><\/div>\n<p>.<\/p>\n<p>Besides giving us much shorter code (more readable, less clutter, friendlier to inlining new objects), this method also encourages use of autoreleased objects. \u00c2\u00a0To really go into why I think this is a good thing, we need another blog post; but I can say briefly that autorelease is the Objective coder&#8217;s smart pointer. \u00c2\u00a0It helps the code, not the coder, remember what needs cleaning up.<\/p>\n<p>Without further ado, here&#8217;s the extremely simple yet incredibly useful category:<\/p>\n<p><a href=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/04\/be_h.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-34\" title=\"be_h\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/04\/be_h.png\" alt=\"\" width=\"175\" height=\"83\" \/><\/a><\/p>\n<p><a href=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/04\/be_m.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-35\" title=\"be_m\" src=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/04\/be_m.png\" alt=\"\" width=\"306\" height=\"122\" srcset=\"http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/04\/be_m.png 306w, http:\/\/bynomial.com\/blog\/wp-content\/uploads\/2010\/04\/be_m-300x119.png 300w\" sizes=\"(max-width: 306px) 100vw, 306px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post explains one of my favorite NSObject categories &#8211; one that is extremely universal and handy for shorter and, in my opinion, more memory-manageable\u00c2\u00a0code. \u00c2\u00a0(Code below.) Edit: Commenter David found a bug in this implementation. Check out the fixed version here; the new source files are listed at the bottom of that post. (The [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[47,40,10,27],"tags":[42,103,41],"_links":{"self":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/33"}],"collection":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33"}],"version-history":[{"count":0,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/33\/revisions"}],"wp:attachment":[{"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bynomial.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}