On May 3, 2012, at 03:52 , Koen van der Drift wrote:

> Thanks! I was indeed using remove<key>AtIndexes: , but set<Key> seems a bit 
> easier to do.

The setter is not quite so easy at it seems. You should declare the property 
with the 'copy' option, so that the parameter is copied by any @synthesized 
setter. If you write your own setter, then you need to make sure you make the 
copy yourself. Otherwise, the caller can (in effect) change your array ivar at 
will (by passing a NSMutableArray object to the setter, and mutating it later).

Also, at the moment you make the copy, there are *three* arrays in existence: 
your current ivar value, the copy (your new ivar value), and the parameter to 
the setter. If the arrays are large or otherwise expensive to create, then the 
copying and memory consumption of the extra copies might be uncomfortably 
unperformant.

In fact, depending on the expected sizes of the arrays involved, the "best" 
implementation of the setter might actually be one that avoids making the copy:

        - (void) setMyArray: (NSArray*) myArray {
                [myArrayIvar removeObjectsAtIndexes: … index set of all 
elements …];
                [myArrayIvar addObjectsFromArray: myArray];
        }

IOW, it would use the wordier code technique that you started out to avoid. :)


_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to