Re: indexed accessor properties and NSArray/NSMutableArray's
Re: indexed accessor properties and NSArray/NSMutableArray's
- Subject: Re: indexed accessor properties and NSArray/NSMutableArray's
- From: Roland King <email@hidden>
- Date: Wed, 06 Aug 2008 01:11:10 +0800
Roland,
How are you mutating your array? If you do [[myObject
mutableArrayValueForKey:@"myArray"] addObject:anObject], it should
preserve the current NSMutableArray even without indexed accessors.
This seems to work in my testing, at least.
Yup, that works, because the methods -mutableArrayValueForKey:, -
mutableArrayValueForKeyPath:, -mutableSetValueForKey: and -
mutableSetValueForKeyPath: manually triggers the notifications.
not for me it doesn't, see my other reply, without the mutator methods
defined in the class, it doesn't preserve the NSMutableArray, it
changes it for a new array.
Note none of my mail has been about triggering notifications nor have
I been talking about using 'behind the scenes' ways of changing
arrays. My mail has been only about how, when you use a correct KVC
pattern, a binding or mutableArrayValueForKey: and the property you're
mutating happens to actually be an Array, it actually ends up getting
mutated when you do, or do not, provide the mutable accessor methods.
But:
1. I thought, that it is a "miracle" to him, why only the specified
patterns work, but no other.
Not so. The documentation says this
"The key-value coding methods mutableArrayValueForKey: and
mutableArrayValueForKeyPath: provide mutable access to a to-many
relationship, regardless of the class used to model the relationship.
In order to support these methods, your class must implement two
additional methods for each of these keys: -
insertObject:in<Key>AtIndex: and -removeObjectFrom<Key>AtIndex:."
however in the case of an NSArray property, even if you do not
implement them, mutableArrayValueForKey: still provides mutable access
to the contents of the array property, albeit by changing the entire
property for a brand new array. The specified pattern says I *must*
implement those methods for it to work, however it worked even when I
didn't, I went to find out why.
2.1. If -mutableCollectionValueForKey: should work efficient, you
have to return a mutable ivar on your accessors (getter). This
means, that *everyone* can change items in that collection without
triggering kvo. This is the opposition of encapsulation and very,
very dangerous.
That I agree with - and my test in the last mail seems to indicate
that encapsulation is preserved. I wasn't able to get
mutable<anything> to change the array directly. That's good.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden