Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C with ARC: Custom setter does not retain
    primarykey
    data
    text
    <blockquote> <p><strong>NOTE: This was due to a bug in some XCode beta versions that has long been fixed. This Question and answer will probably not help you if you have problems with ARC.</strong></p> </blockquote> <hr> <p>I am migrating my project from manual reference counting to ARC, and have stumbled upon a problem: <strong>How can I ensure that a custom setter for a retain property actually retains?</strong></p> <p>In <code>myClass.h</code>, I've declared a property: <code>@property (retain) NSDate *date</code>. It doesn't matter whether I manually set a <code>__strong</code> ivar or have it autogenerated.</p> <p>In the implementation, I have of course <code>@synthesize date</code>, and implemented a custom setter (or just <a href="https://github.com/fzwo/ARC-SetterTest" rel="nofollow">download the demo Xcode project</a>):</p> <pre><code>- (void)setDate:(NSDate *)newDate { if (allowedToSetNewDate) { date = newDate; } } </code></pre> <p>This does not seem to retain the date, and gives me <code>message sent to deallocated instance</code> when newName is (auto-) released where it came from, when trying to access <code>myClass.date</code> later (provided Zombie is enabled; otherwise, it just crashes silently).</p> <p>Changing the setter to use <code>date = [newDate copy]</code> works around the error, but isn't really what I want. Deleting the custom setter also works, but is obviously not desireable.</p> <p>What am I missing here? How can I ensure that a custom setter for a retain property actually retains in an ARC environment? This seems such a basic and common task that I think I'm overlooking something very obvious.</p> <p>(NOTE: This does not fall under the terms of any Apple NDA, as ARC is publically released as part of LLVM)</p> <p>EDIT: I've created a small Xcode project to demo the issue and uploaded it to github. Feel free to <a href="https://github.com/fzwo/ARC-SetterTest" rel="nofollow">download it</a> and play around. I'm at my wit's end (though my wit is not at its best today, admittedly).</p> <p>EDIT: For this sample project, this problem is solved (see accepted answer). Unfortunately, in the bigger project, which I am not at liberty to share, the problem persists. As a workaround, I've added duplicate <code>strong</code> properties with synthesized setters (ivars don't work). The new custom setter now looks like this:</p> <pre><code>- (void)setDate:(NSDate *)newDate { if (allowedToSetNewDate) { self.date_arcretain = newDate; //this property is only there as a workaround. ARC properly retains it, but only if the setter is synthesized date = newDate; } } </code></pre>
    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.
 

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