Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, here's a suggestion.</p> <pre><code>MyParameter *param = [[MyParam alloc] init]; MyObject *obj = [[MyObject alloc] initWithParam:param]; // do you know if param is retain'd or not? [param release]; </code></pre> <p>Why do it this way? This is the pattern used throughout ObjC/Cocoa. You add objects to NSArrays this way:</p> <pre><code>MyThing *thing = [[MyThing alloc] init]; [myMutableArray addObject: thing]; // this calls [thing retain] [thing release]; </code></pre> <p>You may also want to try to do this:</p> <pre><code>MyObject *obj = [[MyObject alloc] initWithParam: [MyParameter parameter]]; </code></pre> <p>where:</p> <pre><code>+ (id) parameter { return [[[self alloc] init] autorelease]; } </code></pre> <p>that way you don't have to worry about it. This is the same as</p> <pre><code>[NSData data]; [NSArray array]; </code></pre> <p>I hope that helps. Generally, it isn't a good idea to use <code>init</code> during another method call (like a setter or another init). Hiding it behind a Class Method (<code>+ (id) parameter</code>) means the user knows it'll be <code>autorelease</code>'d.</p> <p>If you're ever unclear about how many <code>retain</code>'s or <code>release</code>'s something has, you can always do something like this:</p> <hr> <p>[EDIT]</p> <p>Caveat: Apparently you <a href="https://stackoverflow.com/questions/4636146/when-to-use-retaincount">should never use <code>-retainCount</code></a>. I find it useful for teaching learners the basics of <code>retain</code>'d Memory Management, but the point is well taken.</p> <p><strong><a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/retainCount" rel="nofollow noreferrer">Never Use it in actual code or for performance testing.</a> This is only used to learn what <code>retain</code> does (and I believe it functions properly in this case). It will never give you an intelligent answer for an object which is <code>autorelease</code>'d or further <code>retain</code>'d by a <code>NSArray</code> or Other Foundation/AppKit/UIKit Classes</strong></p> <pre><code>MyParameter *param = [[MyParam alloc] init]; NSLog(@"param retain count: %d",[param retainCount]); // should be 1 MyObject *obj = [[MyObject alloc] initWithParam:param]; NSLog(@"param retain count: %d",[param retainCount]); // should be 2, if MyObject retains it. [param release]; NSLog(@"param retain count: %d",[param retainCount]); // should be 1 </code></pre> <hr> <p>Also, when you <code>dealloc</code> MyObject, you'll need to <code>release</code> param if you <code>retain</code>'d it during initialization.</p> <p>The following <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994" rel="nofollow noreferrer">guide</a> put out by Apple should help you to understand Objective-C Memory Management a little better.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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