Possible Duplicate:
Difference between calling self.var vs var
Edit : actually, i did not understand that to add an object to an array does not require a “setter”, but actually a “getter”, so the use of “self” was not important.
can you tell me why here in this code we use sometimes “self.” and sometimes we don’t : if we have a “message” to send to the property, we usually use “self.”, but here in the locationManager delegate for exemple, we don’t use it :
- (void)viewDidLoad {
self.locationMeasurements = [NSMutableArray array];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[locationMeasurements addObject:newLocation];
- (void)viewDidUnload {
locationDetailViewController = nil;
}
- (void)reset {
[self.locationMeasurements removeAllObjects];
}
Thanks for your help
Paul
when you use self you pass through the accessor methods, thereby exploiting the property attributes such as non atomic (or atomic) when reading, or copy, retain,assign when assigning.
When you do not use self you access the variable directly.
Example:
will retain the object in the property
will NOT retain it, the object will be released later on.
self. is syntactic sugar for:
the attributes in your property declaration define how those methods are implemented behind the scenes
In your example code in viewDidLoad the self. is probably needed to make sure the array is retained after assigning. However in “reset” it is probably not needed as you just manipulate the object itself and not the assignment to the property (unless you deal with multithreading and atomic accessors).
One thing to note is that if you create something in ViewDidLoad (the MutableArray) you should destroy it in viewDidUnload ( self.locationMeasurements = nil; )