{"id":1488,"date":"2013-12-22T11:10:06","date_gmt":"2013-12-22T19:10:06","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1488"},"modified":"2013-12-22T11:10:06","modified_gmt":"2013-12-22T19:10:06","slug":"lazy-instantiation-in-ios","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1488","title":{"rendered":"Lazy Instantiation in iOS"},"content":{"rendered":"<p>I had a bunch of code like this in the init for my first view controller. Paul Hegerty had mentioned lazy instantiation in his Stanford CS193 courses. And the boilerplate code in the AppDelegate for creating Managed Object Contexts and Persistent Store Coordinator uses it a log. So I understood the concept, but I hadn\u2019t used it for my own globals. After listening to his most recent class, I decided to convert all of my init code to lazy instantiation.<\/p>\n<p>One benefit of lazy instantiation is that you don\u2019t allocate resources until you need to use the object. In my case, a better reason is that I\u2019m not cluttering up my view controller with code that initializes global variables. In the Model, View, Controller design pattern, initialization code really doesn\u2019t belong in the controller. But even more important, since I check for initialization in the class that creates the variable, I can\u2019t forget to initialize the variable.<\/p>\n<h3>Old Code<\/h3>\n<p><pre><code class=\"preserve-code-formatting\">\n&nbsp;&nbsp;&nbsp;&nbsp;if (![Globals sharedInstance].showmePict ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[Globals sharedInstance].showmePict = @&quot;Either&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;if (![Globals sharedInstance].targetSoundDelay ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[Globals sharedInstance] resetTargetDelay:TARGET_SOUND_DELAY];\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;if ( !([Globals sharedInstance].targetSound) ){\n#ifdef SHOWME_TARGET_SOUND\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[Globals sharedInstance] resetTargetSound:SHOWME_TARGET_SOUND];\n#endif\n&nbsp;&nbsp;&nbsp;&nbsp;\n#ifndef SHOWME_TARGET_SOUND\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[Globals sharedInstance] resetTargetSound:@&quot;&quot;];\n#endif\n&nbsp;&nbsp;&nbsp;&nbsp;}\n<\/code><\/pre><\/p>\n<h3>New Code<\/h3>\n<p>In the singleton for globals.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n- (NSString *)showmePict {\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;if (!_showmePict ) _showmePict = @&quot;Either&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;return _showmePict;\n}\n\n- (NSUInteger)targetSoundDelay {\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;if ( !_targetSoundDelay ) [self resetTargetDelay:TARGET_SOUND_DELAY];\n&nbsp;&nbsp;&nbsp;&nbsp;return _targetSoundDelay;\n}\n\n- (NSString *)targetSound {\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;if ( !_targetSound ){\n&nbsp;&nbsp;&nbsp;&nbsp;#ifdef SHOWME_TARGET_SOUND\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self resetTargetSound:SHOWME_TARGET_SOUND];\n&nbsp;&nbsp;&nbsp;&nbsp;#endif\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#ifndef SHOWME_TARGET_SOUND\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self resetTargetSound:@&quot;&quot;];\n&nbsp;&nbsp;&nbsp;&nbsp;#endif\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;return _targetSound;\n}\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a bunch of code like this in the init for my first view controller. Paul Hegerty had mentioned lazy instantiation in his Stanford CS193 courses. And the boilerplate code in the AppDelegate for creating Managed Object Contexts and Persistent Store Coordinator uses it a log. So I understood the concept, but I hadn\u2019t &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1488\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Lazy Instantiation in iOS<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-1488","post","type-post","status-publish","format-standard","hentry","category-ios"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1488"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1488\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}