Note: Typically in a dealloc method you should release object instance variables directly (rather than invoking a set accessor and passing nilas the parameter), as illustrated in this example:
- (void)dealloc {
[property release];
[super dealloc];
}
If you are using the modern runtime and synthesizing the instance variable, however, you cannot access the instance variable directly, so you must invoke the accessor method:
- (void)dealloc {
[self setProperty:nil];
[super dealloc];
}
What is modern runtime in iOS application development exactly?
It is possible to access the ivar directly, under the same name as the synthesized property. The
@synthesizedirective creates the ivar on your behalf if one does not already exist, and since that is a compiler directive, the ivar is available at compile-time. See “Runtime Difference” in the Declared Properties chapter of The Objective-C Programming Language. As Abizern noted in a comment, it’s also possible to specify whatever name you like for the ivar:@synthesize coffee=tea;— here,teais the ivar andcoffeethe property.To use the ivar, simply refer to it like any other variable, without using the dot syntax. The following is all perfectly legal and works as expected:
The “modern runtime” was introduced with Mac OS X 10.5 (Leopard) as part of the transition to 64-bit. All versions of iOS use the modern runtime. Synthesized instance variables are a feature of the modern runtime, as noted in the link I provided above.
The other key difference, noted in “Runtime Versions and Platforms” of the Objective-C Runtime Programming Guide, is that instance variables are “non-fragile”. There is a layer of indirection added to ivar storage and access which allows classes to add variables without affecting the storage of derived classes. It also presumably facilitates instance variable synthesis. Greg Parker has an explanation involving kittens, there’s passing reference to it in Mike Ash’s 2009 runtime writeup, and Bavarious here on SO has a swell post about ivar storage and class extensions.
You can see other things that changed, though without explanation, in the “Mac OS X Version 10.5 Delta” chapter of the Objective-C Runtime Reference.