Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could try something different, though. I wouldn't have thought of this more than a couple days ago, but I happened to be reading <a href="http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html" rel="nofollow">Cocoa With Love</a>. In the post linked, he discussed how he made a <code>#define</code> macro that would "generate" the entire class for a singleton into wherever you called the macro from. You can <a href="http://projectswithlove.com/projects/SynthesizeSingleton.h.zip" rel="nofollow">download his code</a> for this (may give ideas on your own implementation).</p> <p>Perhaps something like (<em>Warning: Untested Code Ahead</em>):</p> <pre><code>#define SYNTHESIZE_LAZY_INITIALIZER_FOR_OBJECT(objectName, objectType) \ \ - (objectType *)objectName \ { \ if(!objectName) \ { \ objectName = [[objectType alloc] init]; \ } \ return objectName; \ } \ \ - (void)set##objectName:(objectType *)value \ { \ [value retain]; \ [objectName release]; \ objectName = value; \ } </code></pre> <p>would work? I apologize that I don't have time to properly test it for you, so take that as fair warning that this isn't a quick copy/paste solution. Sorry about that. Hopefully it is still useful! ;)</p> <hr> <p><strong>Example Usage</strong></p> <p>This should work, again <em>Warning: Untested Code Ahead</em>:</p> <p>Header</p> <pre><code>// .... @interface SomeClass : NSObject { NSObject *someObj; } @end </code></pre> <p>Implementation</p> <pre><code>@implementation SomeClass // .... SYNTHESIZE_LAZY_INITIALIZER_FOR_OBJECT(someObj, NSObject); // .... @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload