0

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

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You get the accept since you were first and you evangelized style standardization. Joe was a big help too.
1

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.

4 Comments

With [self.myArray addObject: myString]; the setter is not setting anything to nil correct? The getter just returns myArray. I don't see self doing anything in [self.myArray addObject: myString]; Am I missing something?
It is calling the myArray method to return the myArray variable, think of it as [[self myArray] addObject:myString]; vs [myArray addObject:myString]; on uses a method, one doesnt
Agreed. [self myArray] just returns myArray, and does nothing else in this example, correct? If this is true then self does not really do anything in this situation, correct?
correct! (with it being nonatomic and if it is just @synthesized all self.myArray should be doing is adding method call overhead (generally an insignificant amount))
0

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

Comments

0

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.

2 Comments

- (IBAction)createMyArray may be called more than once, so you would want to use self to release the old array and retain the new, no? You are right though the retain count would be 2 so another release is needed aside from the usual one in dealloc.
Yeah you should encapsulate checking for nil, etc, in the createMyArray and release as needed
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.