According to Apple developer site:Practical Memory Management, in the implementation of custom set method of the retain property is as follows:
@interface Counter : NSObject
@property (nonatomic, retain) NSNumber *count;
@end;
- (void)setCount:(NSNumber *)newCount {
[newCount retain];
[_count release];
// Make the new assignment.
_count = newCount;
}
But many website suggest the first step should be release. For example, in this question:objective c - Explicit getters/setters for @properties (MRC) - Stack Overflow, the answer gives another implementation.
- (void)setCount:(NSNumber *)count {
if (count != _count) {
NSNumber *oldCount = _count;
// retain before releasing the old one, in order to avoid other threads to
// ever accessing a released object through the `_count` pointer.
_count = [count retain];
// safely release the old one.
[oldCount release];
}
}
So I'm doubt the difference between these two implementation. Which one is preferred and why?
Thanks for your attention and answer.