Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The reason you're getting a warning from the analyzer is that a getter method is not required to actually return the exact same object as what you passed in to the setter. For example, imagine the following code:</p> <pre><code>- (void)doSomethingWithAString:(NSString *)aString { self.myName = [[NSString alloc] initWithFormat:@"%@ the Great", aString]; [self.myName release]; } </code></pre> <p>The string is created with an owning method (<code>-init...</code>), so you own it. Then you gave it to the <code>myName</code> property, which took ownership. Now you need to release the ownership you received from the <code>-init...</code> method, which is done by calling <code>-release</code>. Great.</p> <p><strong>The problem with the above code</strong> is that <code>[self.myName release]</code> might not release the same object you passed in to the setter. Imagine if the setter were implemented like this:</p> <pre><code>- (void)setMyName:(NSString *)someString { // Make sure to trim whitespace from my name! NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet]; NSString *strippedString = [someString stringByTrimmingCharactersInSet:whitespaceCharacterSet]; [myName autorelease]; myName = [strippedString retain]; } </code></pre> <p>Note that the object you passed into the setter is <em>not</em> the object that was stored to the backing ivar. When you call <code>[self.myName release]</code>, you're releasing the stripped string, not your original string. The original string was now leaked, and the stripped string has been over-released.</p> <p>In short, <strong>never assume that a getter returns the same object you passed to the setter</strong>.</p>
 

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