Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you allocate an object, its retain count begins at 1. Every time you retain, it goes up by one, and every time you release, it goes down by one. The other factor here is autorelease. When an object is autoreleased in the context of an autorelease pool, its retain count will automatically go down by one when the autorelease pool is drained.</p> <p>In your examples, for the first one, <code>name1</code> and <code>newString1</code> will both be pointers to the <em>same</em> object, with that object's retain count staying the same. It would only go up if you explicitly call retain. Same with <code>name2</code> and <code>newString2</code>. In the second example, you are allocating a <em>new</em> instance of an NSString object, so the new ones, <code>anotherString1</code> and <code>anotherString2</code> would have retain counts of 1. <code>name1</code> and <code>name2</code> would stay the same, again, because you didn't release or retain them.</p> <p>Your second example is essentially the same as <code>copy</code>. While <code>retain</code> simply increments the retain count of an object, <code>copy</code> creates a new one one with its own retain count. The related thing to flag here is in your declaration of <code>name1</code> and <code>name2</code> as properties. If you set those properties, they will behave as I'm describing here with <code>name1</code> retaining the <code>NSString</code> you set there and <code>name2</code> copying it as a new object.</p> <p>Another good overview of all this is <a href="http://developer.apple.com/library/ios/#documentation/general/conceptual/devpedia-cocoacore/MemoryManagement.html" rel="noreferrer">here</a>, with a link to an even more <a href="http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011i" rel="noreferrer">detailed discussion</a> at the bottom.</p> <p>This all being said, in OS X, you can use garbage collection, and on iOS 5, <a href="http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html" rel="noreferrer">automatic reference counting</a>. These can significantly reduce, if not eliminate, your need to worry about this stuff.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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