<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Mon, 17 Aug 2026 11:41:54 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>mikeash - 2014-05-30 17:23:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Good question! If it's reasonable, I'd put an explicit "setup" call somewhere convenient, like your app delegate. Implicit can be nice, but no need to overdo it. Absent that, I'd put a small amount of code in &lt;code&gt;+load&lt;/code&gt; that then use &lt;code&gt;dispatch_async&lt;/code&gt; or &lt;code&gt; CFRunLoopPerformBlock&lt;/code&gt; to schedule the real code for execution once the main runloop starts up. It should be safe to use these APIs from &lt;code&gt;+load&lt;/code&gt; since they're lower level, and frameworks that you link to are guaranteed to be initialized first.</description><guid isPermaLink="true">62a85b483c51322c7ee05205ba9f0374</guid><pubDate>Fri, 30 May 2014 17:23:39 GMT</pubDate></item><item><title>@Ahti333 - 2014-05-30 15:20:01</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>@mikeash Do you have any recommendation for code that needs to run in a sane environment like +initialize, but needs not to run lazily?
&lt;br /&gt;
&lt;br /&gt;My case: A class that needs to register itself to some other class from the same library, so in +load, I can't be sure that this other class is loaded yet, and I can't use +initialize becuase the class might never get messages then.</description><guid isPermaLink="true">1be48ad8f9f86bd9e189b8dec49a2b8a</guid><pubDate>Fri, 30 May 2014 15:20:01 GMT</pubDate></item><item><title>Walt Sellers - 2014-04-30 15:50:49</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>@scottcorscadden
&lt;br /&gt;
&lt;br /&gt;There is no need to add dispatch_once(). It is already provided at least since iOS 6 and presume the same for Mac OS X. You can put a breakpoint in +initialize and see it in the stack trace.
&lt;br /&gt;
&lt;br /&gt;(I recall testing iOS 5 and found that it had acquired a pthread_mutex lock before calling +initialize)
&lt;br /&gt;</description><guid isPermaLink="true">d690eaf993526d3fc7c0517fb1b54ce0</guid><pubDate>Wed, 30 Apr 2014 15:50:49 GMT</pubDate></item><item><title>@scottcorscadden - 2013-10-25 14:46:57</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Hey, a quick update (improvement?) to this with dispatch_once here might be good, per &lt;a href="http://stackoverflow.com/a/11555869/297472"&gt;http://stackoverflow.com/a/11555869/297472&lt;/a&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;+ (void) initialize {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;static dispatch_once_t onceToken;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dispatch_once(&amp;amp;onceToken, ^{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// &amp;lt;do stuff here once, Johnny-Dangerously style&amp;gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;});
&lt;br /&gt;}
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;Great work btw. 
&lt;br /&gt;./scc</description><guid isPermaLink="true">de88ae3254695dac56539ba6c10ca6b8</guid><pubDate>Fri, 25 Oct 2013 14:46:57 GMT</pubDate></item><item><title>Avi - 2013-08-28 13:35:48</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>@bob
&lt;br /&gt;
&lt;br /&gt;That just points to NSCalendar not setting currentCalendar via +load.</description><guid isPermaLink="true">27d8c1456efc4500960cc4836e5618f5</guid><pubDate>Wed, 28 Aug 2013 13:35:48 GMT</pubDate></item><item><title>bob - 2013-04-20 06:06:45</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>@Mike Ash:
&lt;br /&gt;"You really can't call out to any other Objective-C classes, because they may rely on +load too, and yours might run first. "
&lt;br /&gt;
&lt;br /&gt;However, that seems to contradict what you said in your article above: "The good news is that frameworks you link to are guaranteed to be fully loaded by this point, so it's safe to use framework classes." Since Billy Gray's category is in his framework, and it links against Foundation framework, shouldn't that mean NSCalendar's currentCalendar stuff (which is in Foundation) should have already been "+load"ed?</description><guid isPermaLink="true">6af1e831c46c2b5adbd773e0dd188048</guid><pubDate>Sat, 20 Apr 2013 06:06:45 GMT</pubDate></item><item><title>Philippe Converset - 2013-04-19 06:45:35</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Any thought about using +initialize to create a singleton with something like this?
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;static WhateverClass *sharedInstance;
&lt;br /&gt;
&lt;br /&gt;+ (void)initialize
&lt;br /&gt;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(self == [WhateverClass class])
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sharedInstance = [[self alloc] init];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;}
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;It would mean we can get rid of all these ugly dispatch_once! It looks too good to be true! </description><guid isPermaLink="true">4f3aafd64da31721b492837fa53ddaa6</guid><pubDate>Fri, 19 Apr 2013 06:45:35 GMT</pubDate></item><item><title>Billy Gray - 2012-04-04 15:51:20</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Ah, very nice idea, didn't know that was out there. Thanks, Mike!</description><guid isPermaLink="true">ee79ef9b2f7bfa068ed03f757ccb7b9a</guid><pubDate>Wed, 04 Apr 2012 15:51:20 GMT</pubDate></item><item><title>mikeash - 2012-03-31 18:31:51</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>The +load method can be dangerous like that. You really can't call out to any other Objective-C classes, because they may rely on +load too, and yours might run first.
&lt;br /&gt;
&lt;br /&gt;My recommendation would be to use dispatch_once to lazily initialize your static variable. The additional overhead is insignificant and you completely avoid problems like this.</description><guid isPermaLink="true">5572a526c88d5939a69ed83c0b91dadb</guid><pubDate>Sat, 31 Mar 2012 18:31:51 GMT</pubDate></item><item><title>Billy Gray - 2012-03-28 17:28:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>FWIW, I've got a category on NSDate (NSDate+Helper) and at some point one of the contributors added code to use a shared, static NSCalendar object on the category, instantiated in the +load method, by calling [NSCalendar currentCalendar]. This worked fine on Debug configuration (iOS 5.1, Xcode 4.3.1, unsure if earlier versions are affected), but in Release configuration, +load must be getting called earlier than expected, perhaps before there is a currentCalendar, so it was always nil! Devil of a thing to debug, dug it up by sticking some NSLog statements in there to figure out what was going on. Certainly would appreciate any ideas on how it might be approached, for now I just removed the use of a static calendar.
&lt;br /&gt;
&lt;br /&gt;More info here if anyone wants to look at the code:
&lt;br /&gt;&lt;a href="https://github.com/billymeltdown/nsdate-helper/issues/7"&gt;https://github.com/billymeltdown/nsdate-helper/issues/7&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;Cheers!</description><guid isPermaLink="true">300771145ba121cfa2813979b7e54e91</guid><pubDate>Wed, 28 Mar 2012 17:28:25 GMT</pubDate></item><item><title>HCOD Rayman - 2012-03-20 20:46:53</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Ah, I see. You learn something new every day someone teaches you something you didn't already know. Thanks man</description><guid isPermaLink="true">77754245ff2f4b2a0401f7cbf0abba1b</guid><pubDate>Tue, 20 Mar 2012 20:46:53 GMT</pubDate></item><item><title>mikeash - 2012-03-20 14:48:38</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>In a class method, self refers to the class.</description><guid isPermaLink="true">180c99ccce7574408af5bc1f825c04fa</guid><pubDate>Tue, 20 Mar 2012 14:48:38 GMT</pubDate></item><item><title>HCOD Rayman - 2012-03-20 07:47:52</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Im slightly confused here... + (void) initialize is a class method, so how can you access the self variable?</description><guid isPermaLink="true">4a9ce1e691d8f85db5ce2f3fbd29e859</guid><pubDate>Tue, 20 Mar 2012 07:47:52 GMT</pubDate></item><item><title>mikeash - 2011-09-20 19:04:20</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>There is no such prefix-matching behavior, no. I'd suggest spending some quality time with the debugger to figure out what's calling these.</description><guid isPermaLink="true">6f5d032795fd0b1c452bbcc25ae891ab</guid><pubDate>Tue, 20 Sep 2011 19:04:20 GMT</pubDate></item><item><title>PearlWhite88 - 2011-09-20 18:30:56</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Will the function names that starting with initialize and load will also get while loading. Because I m seeing a strange behaviour where a function with name intializeX got called on itself.
&lt;br /&gt;
&lt;br /&gt;Thanks</description><guid isPermaLink="true">09ed325e12eaf349eb49eeee94f50afc</guid><pubDate>Tue, 20 Sep 2011 18:30:56 GMT</pubDate></item><item><title>Steven O'Toole - 2009-05-24 09:32:13</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Thanks Joachim.</description><guid isPermaLink="true">e16e118a189a814d25526d05e06830c2</guid><pubDate>Sun, 24 May 2009 09:32:13 GMT</pubDate></item><item><title>Joachim Bengtsson - 2009-05-24 06:01:34</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Gah, tr.im messed up those urls... The full URLs are
&lt;br /&gt;&lt;a href="http://www.friendlystapler.se/browser/RMS/trunk/Source/Include/Utility/StaticInitializer.h"&gt;http://www.friendlystapler.se/browser/RMS/trunk/Source/Include/Utility/StaticInitializer.h&lt;/a&gt;
&lt;br /&gt;&lt;a href="http://www.friendlystapler.se/browser/RMS/trunk/Source/Implementation/Utility/StaticInitializer.cpp"&gt;http://www.friendlystapler.se/browser/RMS/trunk/Source/Implementation/Utility/StaticInitializer.cpp&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;Sorry for the double post</description><guid isPermaLink="true">3886695da454711aa2181f7419944f43</guid><pubDate>Sun, 24 May 2009 06:01:34 GMT</pubDate></item><item><title>Joachim Bengtsson - 2009-05-24 05:54:19</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Steven: The memory is reclaimed by the application terminating ;) If you really need to do some cleanup (remove cache file or something), register an atexit() function, or subscribe to the applicationWillTerminate notification.
&lt;br /&gt;
&lt;br /&gt;Mike: I love your Q&amp;amp;A's! Didn't know about +load, thanks :)
&lt;br /&gt;
&lt;br /&gt;Back when I "coded" in C++, I wrote a StaticInitialization class that one could inherit from to get the equivalent of +load (it used a macro to define a static int being initialized by a function call). It had a function called require() that would throw an exception if the required class hadn't been +load'ed, which would be caught by that global initialize function and the throwing initializer would be put on a queue to be checked again once the require()'d class was +load'ed. Phew! It actually worked, too!
&lt;br /&gt;Hm, come to think of it, I even have the source here: &lt;a href="http://tr.im/static_h"&gt;http://tr.im/static_h&lt;/a&gt; &lt;a href="http://tr.im/static_cpp"&gt;http://tr.im/static_cpp&lt;/a&gt;</description><guid isPermaLink="true">9a2f9a085f2f13ffacadc707465f4029</guid><pubDate>Sun, 24 May 2009 05:54:19 GMT</pubDate></item><item><title>Steven O'Toole - 2009-05-24 02:04:26</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html#comments</link><description>Thanks for the article.  I have been wondering about +initialize.  I can't find a corresponding de-initialize.  If I allocate objects, where do they get cleaned up?  
&lt;br /&gt;
&lt;br /&gt;For example:
&lt;br /&gt;
&lt;br /&gt;-------
&lt;br /&gt;
&lt;br /&gt;static NSDateFormatter *dateFormatter = nil;
&lt;br /&gt;static NSCalendar *gregorian = nil;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;
&lt;br /&gt;@implementation WhatEver
&lt;br /&gt;
&lt;br /&gt;+ (void)initialize {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(self != [WhatEver class]) { return;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dateFormatter = [[NSDateFormatter alloc] init];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[dateFormatter setDateFormat:@"MMM dd, yyyy"];
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;br /&gt;}
&lt;br /&gt;</description><guid isPermaLink="true">b7033df9a5c1bbb802933a74b587cb1b</guid><pubDate>Sun, 24 May 2009 02:04:26 GMT</pubDate></item></channel></rss>
