Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>the practical differences are as follows.</p> <p>Setting the property to nil by using the property accessor will let the synthesized method take hold of your new nil property after releasing the existing property.</p> <pre><code>// we will take for granted that you synthesize this property @property (nonatomic, retain) IBOutlet UILabel* label; </code></pre> <p>we will use the property accessor and set it to nil.</p> <pre><code>//This will in actuality set the new value of nil to the label variable after //releasing the existing label that it had a retain count on. self.label = nil; </code></pre> <p>next we will release it directly</p> <pre><code>//This line on the other hand will merely release the label directly. //As soon as the label is deallocated you will have a handle to an invalid object. //(memory space that used to be your label) [label release]; </code></pre> <p>now we will show a simplified version of the property accessor. (not to be used literally)</p> <pre><code>//Simply put the following is an pseudo equivalent of the property setter. [label release] label = nil; </code></pre> <p>the main point here being that the property accessor handles releasing the label that it retained. and setting it to whatever you hand it (in this case being nil) </p> <p>therefore adding the following code</p> <pre><code>label = nil; </code></pre> <p>without releasing the retained object would cause a memory leak and you would have a retain count on a label you no longer have a pointer to.</p> <p><strong>Note</strong>:</p> <blockquote> <p>another thing to take into account. Any pointer that is nil. will be able to accept messages. And in return they will reply with nil. An object that was released on the other hand, as soon as that memory is deallocated your message to it will most likely throw an error. the outcome is unpredictable. This is a good reason for setting your properties to nil. Not only will it handle the release for the object that it is holding. but it will also give you an object you can safely message to without blowing up.</p> </blockquote> <p><strong>A good point @WaltSellers</strong></p> <p>Accessing a variable -- regardless if its the property accessor or instance variable. -- After it has been released completely. Will result in "Undefined" actions. This means that the access may act ok, or it may ruin other parts of the app, or alternatively it may just blow up real fast and terminate the offending app. Basically setting the variable to nil after releasing will enable you to get past that error.</p> <p><strong>A separate tip from me</strong></p> <p>to overcome the misconception of property accessor and instance variable I just @synthesize and tell it to set a variable name.</p> <pre><code>@synthesize label = _label; </code></pre> <p>doing this allows me to differentiate self.label from its instance variable. as no longer can you access the label variable directly without the preceding _</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. 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