开发者

Releasing ivars in Objective-C

开发者 https://www.devze.com 2023-03-01 16:24 出处:网络
OK, looking at this: Apple docs: Declared Properties If you scroll down to dealloc it reads: \"Typically in a dealloc method you should release object instance variables directly (rather than invok

OK, looking at this:

Apple docs: Declared Properties

If you scroll down to dealloc it reads:

"Typically in a dealloc method you should release object instance variables directly (rather than invoking a set accessor and passing nil as the parameter), as illustrated in this example:"

- (void)dealloc {
    [property release];
    [super dealloc];
}

"If you are using the modern runtime and synthesizing the i开发者_如何转开发nstance variable, however, you cannot access the instance variable directly, so you must invoke the accessor method:"

- (void)dealloc {
    [self setProperty:nil];
    [super dealloc];
}

Now, I must own at least 15 to 20 books on iOS development. I can't say that I have ever seen any code in these books proposing that one do anything other than:

[someproperty release];

Is there a compelling reason to edit a bunch of files of code that works perfectly well to adopt Apple's recommendation? How about future work? Or, are they pretty much equivalent?


Apple's document appears to apply only if you have synthesize-by-default turned on. If you have it turned off, which seems to be the default case, you need @synthesize, which gives you access to variables directly.

Personally, I do:

@synthesize someProperty=_someProperty;

just so I can then do:

- (void)dealloc {
  [_someProperty release];
  [super dealloc];
}

See Jeff LaMarche's blog for more.


On the modern runtime, you no longer have to declare instance variables for properties, nor do you have to write @synthesize in the implementation. If your code already has the instance variables declared, then there's no reason to replace code that calls release with a call to a set accessor (the first quote you pasted even states that).

0

精彩评论

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

关注公众号