Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0
I’m using properties only for ivars that don’t require any real validation when getting or setting a value for a class that I’m authoring.
Ex:
@interface ClassB : NSObject
{
// No ivars
}
@property (nonatomic, retain) ClassA *obj;
@property (nonatomic, retain) NSString *str;
Whenever I call these methods within the class I’m always using self.obj/self.str or [self obj]/[self str]. This way in order to set the state of the object you are forced to go through the property so that the references are more carefully managed by the compiler produced getters and setters. With that said, are there any problems that I can run into creating my classes this way? Also, with no ivar present, is any “release” needed in an overwritten “dealloc” method in the @implementation file?
If you’re using the @synthesize directive to create the property accessors, the compiler will also create any ivars that you leave out of the class declaration. You do still need to release your ivars. It used to be that you couldn’t access synthesized ivars directly and so had to use the property setter in your -dealloc method like this:
These days, though, the compiler will let you access the synthesized ivar directly, so you can release it instead of calling the setter (which is a good idea in -dealloc, and a bad idea everywhere else):
This will change when you eventually convert your code for ARC (automatic reference counting) because the compiler will take care of releasing your ivars in -dealloc for you. Most of the time, this means that your -dealloc implementation can just go away.