is this line of code correct re memory management (re NSDate copy)?
is this line of code correct re memory management (re NSDate copy)?
I have a class with properties:
@property (nonatomic, retain) NSDate* start;
@property (nonatomic, retain) NSDate* coreWeStart;
Now in the init method, assuming self.start is already set, is this correct re setting the coreWeStart to the same date:
self.coreWeStart= [[self.start copy] autorelease];
Just double che开发者_如何学运维cking my understanding that:
- needs a 'copy' as otherwise it would refer to the same object and
- needs an autorelease as I did do a copy
thanks
I would say kind of, but it could still be done better. Specifically, you could do:
@property (nonatomic, copy) NSDate* coreWeStart;
...and then:
self.coreWeStart = self.start;
...to get the same thing with less code. Also be sure to do self.coreWeStart = nil
in dealloc (and self.start = nil
too).
Yep. You got it.
- Copy returns a new object with a retain count of one.
- assigning it to the retain keyword property will increment the retain count.
- autorelease will decrement the retain count.
So your object has the coreWeStart property with a retain count of one, which is a copy of the start property.
精彩评论