2

Is checking the new value against the old value still necessary under ARC in a setter? I don't remember when or where (I think it was in another Stack Overflow question) I read that it was no longer necessary.

- (void)setProperty:(PropertyType)property
{
    if (_property != property) {
        _property = property;
    }
    // Other code
}

Can someone tell me where can I find official documentation on this?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Alex Takashi Tanabe
  • 129
  • 1
  • 3
  • 11
  • Basically, I want to have access to any apple documentation that confirms that there is no need for that condition. I don't want you to feel that I don't believe your answers, is just that this is for my company documentation and I need those kind of sources =( – Alex Takashi Tanabe Jan 09 '14 at 19:42
  • Is this a setter for a property? Why not just let it be synthesized? Or do you need to do more than is shown in your code? – rmaddy Jan 09 '14 at 19:49
  • Yep, there is more. I will put that in the code =) Thanks for noticing it. – Alex Takashi Tanabe Jan 09 '14 at 19:51
  • Not a duplicate since he's asking for official documentation location - not how to do it. – Paul Beusterien Jan 09 '14 at 22:39

2 Answers2

3
- (void)setProperty:(PropertyType)property
{
    _property = property;
}

This is sufficient for a setter in ARC.

danielM
  • 2,462
  • 2
  • 20
  • 21
  • Do you know any documentation that says that the condition is not needed? – Alex Takashi Tanabe Jan 09 '14 at 19:43
  • I didn't find documentation. However, ARC will be able to handle anything related to the memory associated with that property assignment. – danielM Jan 09 '14 at 19:49
  • Maybe you can tell me the reason that 'if' statement was used in earlier versions of XCode, before ARC. I need to know the reason behind why it is not needed anymore =) – Alex Takashi Tanabe Jan 09 '14 at 19:52
  • 3
    The condition was used in the past because if the objects were different, you'd release the old one before assigning and retaining the new object. If the objects were the same, on the other hand, you wouldn't want to release the "old" object because that could cause the object to be destroyed instantly, which isn't exactly desirable if you're trying to set the property to that same object. – Caleb Jan 09 '14 at 20:00
1

Assuming PropertyType is a pointer to Objective-C object, you could always have written your setter with no checking, like this:

- (void)setProperty:(PropertyType)newValue {
    [newValue retain];
    PropertyType oldValue = _property;
    _property = newValue;
    [oldValue release];
}

ARC implements strong assignment exactly like that. Quoting Objective-C Automatic Reference Counting from the clang documentation:

For __strong objects, the new pointee is first retained; second, the lvalue is loaded with primitive semantics; third, the new pointee is stored into the lvalue with primitive semantics; and finally, the old pointee is released. This is not performed atomically; external synchronization must be used to make this safe in the face of concurrent loads and stores.

Thus we can deduce that, under ARC, you can implement a property setter with a simple assignment and no checking:

- (void)setProperty:(PropertyType)newValue {
    _property = newValue;
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848