0

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

Community
  • 1
  • 1
Paul
  • 6,108
  • 14
  • 72
  • 128
  • 1
    This **viewDidUnload** method is probably going to cause a memory leak. – Maurício Linhares Jul 27 '11 at 20:03
  • You may find this useful: http://stackoverflow.com/questions/2385980/objective-c-when-to-use-self – Luke Jul 27 '11 at 20:03
  • Thanks for the links, i'm also wondering why we use first "self." and then for the same property we chose to set "directly" the ivar, without using the accessors... – Paul Jul 27 '11 at 21:39

2 Answers2

1

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:

@interface X : NSObject
{
    NSObject * anObject;
}
@property(nonatomic,retain) NSObject * anObject;

self.anObject = [[NSObject alloc]init]autorelease];

will retain the object in the property

anobject = [[NSObject alloc]init]autorelease];

will NOT retain it, the object will be released later on.

self. is syntactic sugar for:

-(NSObject *)anObject;

and

-(void)setAnObject:(NSObject *)other;

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; )

Joris Mans
  • 6,024
  • 6
  • 42
  • 69
  • Thanks Joris, i don't really understand it in my exemple because it's the same "ivar" or "property" that is "set" one time with self. and afterwards without this "self." .Like, it's one or the other way, but in my code, the same "locationMeasurement" is using both ways. Could you explain me why? – Paul Jul 27 '11 at 21:36
  • I added something in my answer. Hopefully this helps. – Joris Mans Jul 28 '11 at 07:15
  • thanks, but there's still something i don't understand : to modify an ivar, we need a "setter" right? so here, we're modifying the array, by adding a new item with "addObject", so the ivar will be edited, but it does not take the "self." to edit the ivar through the setter, it just "addObject" a new item directly on the ivar... without the "self.", and this is where i don't understand – Paul Jul 28 '11 at 17:30
  • You do not modify the ivar when you add an object to the array. You modify the object the ivar points to. Modifying the ivar is when you assign another array to it e.g. self.locationMeasurements = [NSMutableArray array]. The ivar contains just a pointer value pointing to the actual object. It does not contain the object itself – Joris Mans Jul 28 '11 at 17:44
  • alright ok it makes sense now, i did not understand why, in an array, we did not use the "self.", because it's actually a getter. Alright so it's all about memory management, we can modify an object in the .m file without "self" because we directly change something inside the object, but if we change its "lifetime", we actually use the setter method from the property. Right? (Edit: if we do : myVar = smthg; so we erase the first "object", and then "create" one, so we use a setter in this case, because we change the lifetime of "myVar"? alright, if you tell me it is so, i finally understand it! – Paul Jul 28 '11 at 19:53
  • I think you start to understand the principle :) – Joris Mans Jul 29 '11 at 14:43
0

When using self. you get all of the benefits of the @property declaration. This includes the memory managment (copying,retaing,assigning and releasing) and the atomicity. When not using self and there is an instance variable with the same name you lose the benefits and this especially effected when assigning the variable. Your viewDidUnload for example will cause a leak if locationDetailViewController was declared as copy or retain since you did not use self.

Joe
  • 56,979
  • 9
  • 128
  • 135
  • thanks Joe, as for my comment in the previous answer, i'm asking it to you : i don't really understand the use or not of self. in my exemple, because it's the same "ivar" or "property" that is "set" one time with self. and afterwards without "self." : i would think that it's one or the other way, but in my code, the same "locationMeasurement" is using both ways. Could you explain me this? Thanks again – Paul Jul 27 '11 at 21:38