When inserting an object into an array with a property is there any reason to invoke the getter/setter with self? i.e.
[self.myArray insertObject: myObject];
Or can I just use:
[myArray insertObject: myObject];
the gist would be:
.h
@interface ArrayViewController : UIViewController <UITextFieldDelegate>
{
NSMutableArray *myArray;
int itemNumber;
}
@property (nonatomic, retain) NSMutableArray *myArray;
@end
.m
- (IBAction)createMyArray
{
self.myArray = [[NSMutableArray alloc] initWithObjects: nil];
}
-(IBAction) addItemToMyArray
{
NSString *myString = [[NSString alloc] initWithFormat:@"item %d",itemNumber];
[myArray addObject: myString];
//[self.myArray addObject: myString]; //Or should I use self?
[myString release];
NSLog(@"myArray = %@", myArray);
itemNumber++;
}
//- (void)dealloc etc. not shown
Conceptually, it doesn’t matter, so long as your getter method only returns the existing field value and doesn’t, eg, do some “just in time” allocation or some such.
However, it’s good practice to come up with a policy (personal or group) that you stick with, so that the caveats of that policy become second nature. Constantly switching styles results in sloppy, buggy code.
I tend to always use the
self.for properties, just to remind myself that they are, in fact, properties, and to make it less likely that I’ll accidentally set the value without using the property notation.