Reputation: 10738
why would you use @property
with NSArray
?
The reason for my question is because I was under the impression that @properties
were mainly used to take advantage of the getter and setter you get for free when you use @property
, but when you assign an NSArray
to a property you know you will never use the getter and setter to access the objects in the array, why use properties on this case. Does this have to do with ARC, in other words is this considered a better memory management practice?
Upvotes: 0
Views: 115
Reputation: 124997
but when you assign an NSArray to a property you know you will never use the getter and setter to access the objects in the array, why use properties on this case.
For the same reasons that you use properties for other objects:
accessors: The accessors are for the array itself, not objects in the array. It's not uncommon to get or set an entire array at once:
NSArray *kids = frank.children; // get the array mary.children = @[@"Nick", @"Sam", @"Susan"]; // set the array
abstraction: Using properties instead of accessing the underlying array directly makes it easier to change the implementation later, should that become necessary.
KVC compliance: You get it for free with properties. With collections like arrays, KVC gives you a lot of power. You can use collection operators like @avg
and @max
as well as indexed accessors:
int employees = [accounting countOfEmployees]; // same as [accounting.employees count] Employee *thirdEmployee = [engineering objectInEmployeesAtIndex:2]; // same as [engineering.employees objectAtIndex:2]
Upvotes: 2
Reputation: 108101
Along with what already said by other answers, I usually like to copy
my array for security purposes.
If you're accepting an NSArray
by the client, she could potentially provide you a NSMutableArray
instance. If that's the case and you want to ensure that the array you're working with doesn't change unexpectedly, you'd better copy
it as opposed to retain
it.
Properties come in handy in this case since you can declare
@property (nonatomic, copy) NSArray * myArray;
and be sure that you're the only owner of that array after you assigned it.
I typically use this strategy for any class with mutable subclasses, such as NSString
, NSArray
, NSSet
and so on, whenever I care about the ownership of the data.
The downside of this strategy is of course memory efficiency, but in the end engineering is the art of intelligent compromise.
Upvotes: 3
Reputation: 318804
You use a property to get or set the whole array. You also use the property to access individual elements of the array.
Using a property with an array has nothing to do with ARC or MRC. Properties are a way to encapsulate data. That's it.
If you don't wish to provide access to the whole array then don't use a property. Add method(s) to set/get individual elements of the internal array if that is appropriate.
Upvotes: 4
Reputation: 9414
To use the array outside of a single method or class. ClassA.array = ClassB.array; or to simply read from the array in a different method of the same class since the array will dealloc immediately after execution.
Upvotes: 2