开发者

is this line of code correct re memory management (re NSDate copy)?

开发者 https://www.devze.com 2023-03-03 03:53 出处:网络
is this line of code correct re memory management (re NSDate copy)? I have a class with properties: @property (nonatomic, retain) NSDate* start;

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:

  1. needs a 'copy' as otherwise it would refer to the same object and
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消