Goal C: The correct way to initialize NSArray, which is @property

I have a property in my class that is an NSArray. I keep the property.

My question is: what is the correct way to add objects to this array without leakage and is the retention rate too high?

This is what I use:

.h: NSArray *foodLocations; @property (nonatomic, retain) NSArray *foodLocations; 

// I'm sure to synthesize and release a property in my dealloc.

 .m - (void)viewDidLoad { [super viewDidLoad]; NSArray *tempFood = [[NSArray alloc] initWithArray:[self returnOtherArray]]; self.foodLocations = tempFood; [tempFood release]; } 

Is this being done right?

+7
source share
5 answers

Yes, this is correct and my preferred way to do this is because it makes the code more readable.

You essentially allocate a temporary array, and then assign it to a property with a save attribute, so it's safe to free it, since your property now "owns" it. Just remember that you still need to free it in your dealloc method.

You can also initialize the array and assign it to a property in the view init controller method, depending on whether you need the property to be available to you before the view actually loads (that is, if you want to read the value of the property before clicking the view controller etc.)

+7
source

you usually want to declare a copy of the property in this case.

in most cases, immutable collectors should be copied, not saved. many people make mistakes and end up writing a lot of copies by hand and exchange objects that should not be shared, thinking that they are doing everything well by cutting a corner.

copying in this form (collection) shallow. objects in the array are not copied, but only the distribution of the array.

a good implementation of an immutable collection can simply implement a copy while preserving the self. if the argument is changed, you want to get a copy anyway (in most cases).

your program is then simplified to an announcement:

 // note: copy, not retain. honor this if you implement the accessors. @property (nonatomic, copy) NSArray * foodLocations; 

and then the setter:

 self.foodLocations = [self returnOtherArray]; 

Of course, you should still initialize, dealloc, and properly manage streaming security.

luck

+4
source

It looks good. You really don't need the tempFood variable, you can just do:

 self.foodLocations = [[NSArray alloc] initWithArray:[self returnOtherArray]]; [self.foodLocations release]; 

or

 self.foodLocations = [[[NSArray alloc] initWithArray:[self returnOtherArray]] autorelease]; 
+3
source

Or:

 @synthesize foodLocations=_foodLocations; 

then in code

 _foodLocations = [[NSArray alloc] initWithArray:someOtherArray]; 

This avoids the auto ads required

 self.foodLocations = [[[NSArray alloc] initWithArray:someOtherArray] autorelease]; 
+3
source

Yes, that's right. It's also good to keep in mind what @synthesize essentially does for you. The synthesized (& saved) setter is functionally equivalent to the following code:

 - (void)setVar:(id)_var { [_var retain]; [var release]; var = _var; [var retain]; [_var release]; } 

So basically, every time you call self.var = foo, it frees the previously saved value and saves the new one. You process the reference count in your code, and the setter processes your own.

+2
source

All Articles