Cephi
Cephi

Reputation: 221

Self with an array and addObject

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

Upvotes: 0

Views: 334

Answers (6)

Hot Licks
Hot Licks

Reputation: 47729

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.

Upvotes: 1

Lokus001
Lokus001

Reputation: 159

In your case it's not an obligation to use self.myArray but for this case belloaw it will be an obligation:

-(void) addItemToMyArray:(NSAarray *)myArray
{

    NSString *myString = [[NSString alloc] initWithFormat:@"item %d",itemNumber];

    [self.myArray addObject: myString];
    [myString release];

    NSLog(@"myArray = %@", self.myArray);

    itemNumber++;
}

to difference between the class attribut and the function argument.

Upvotes: 0

Tim Dean
Tim Dean

Reputation: 8292

I typically skip the getter because I rarely find it valuable and it clutters up the readability of the code a bit. However, I tend to use the setter because I find it easier to allow the auto-generated setter methods to handle the retain/release semantics

Upvotes: 0

shawnwall
shawnwall

Reputation: 4607

When using alloc/init you should not set the returned value to a property, as these will retain twice:

self.myArray = [[NSMutableArray alloc] initWithObjects: nil];

use

myArray = [[NSMutableArray alloc] initWithObjects: nil];

or

self.myArray = [NSMutableArray array];

for the initialization.

The insert operations are equivalent though.

Upvotes: 0

Joe
Joe

Reputation: 57179

Either will work but you need to be aware of what you are doing. Using self. will invoke the setter/getter methods while the other will just access the variable directly. Using the variable directly, while perfectly valid, is discouraged outside of the initializer and dealloc method. The reason is you are losing out on the benefits of the property, especially setting using self. because it will properly assign/copy/retain the value for you correctly. Another reason not use property variables directly is because of atomicity but in your case you declared it as nonatomic.

Upvotes: 1

jtbandes
jtbandes

Reputation: 118691

Both of those are fine. It's mostly a stylistic choice. Using self.myArray will result in a call to the getter [self myArray].

Upvotes: 0

Related Questions