Java allows access to object variables using the dot notation as in:
myObject.someValue= someNewValue;
SomeValue value= myObject.someValue;
Objective C supports the concept of a property. You can use properties to set and get instance values using the dot notation as in:
myClass.myValue= someValue;
SomeValue *value= myClass.myValue;
When you use properties, you can let the compiler worry about calling retain and release on assignment. You still need to call release in dealloc on a property.
-(void)dealloc {
[myValue release];
[super dealloc];
}
You will need to use the compiler calls @property in the header file:
@interface MyClass : NSObject {
NSString *myString;
}
@property (nonanatomic, retain) NSString *myString;
@end
and @synthesize in the implementation file:
@implementation MyClass
@synthesize myString;
@end
to create a property.
It may be wise to use the . notation on self.someProperty as the call:
someProperty= someValue;
does NOT add one to the retain count. Here are some examples:
self.someProperty= someValue; // compiles and adds retain count of one
someProperty = someValue; // compiles and does NOT add one to the retain count
self.notAProperty= someValue; // does NOT compile
notAProperty= someValue; // compiles, but does NOT add a retain count
USAGE:
self.someProperty= someValueAllocInitAutorelease;
someProperty= someValueAllocInit; // probably not a good idea to do this
On dealloc:
[someProperty release];
SomeValue* temp= SomeValueAllocInit;
self.someProperty= temp;
[temp release];
notAProperty= SomeValueAllocInit;
On dealloc:
[notAProperty release];
You can verify this with code as in:
if (IS_DEBUG) NSLog(@"vcMain retain count is %i",[self.vcMain retainCount]);
MORAL: If you declare a property, consider using only the . notation to touch the property.
IBOutlets: According to the docs if your view controller has an IBOutlet that is connect to a xib you are to call release on the IBOutlet in viewDidUnload which can happen on a low memory warning. This seems to crash my app. The default implementation suggest instead self.myOutlet= nil; which may call release if necessary.