Setting values by key vs. by method
Setting values by key vs. by method
- Subject: Setting values by key vs. by method
- From: Dustin Voss <email@hidden>
- Date: Sat, 8 Nov 2003 14:24:22 -0800
Two of the ways to set a property of an object is via a mutator method
(e.g. setFoobaz:) or via key-value coding (e.g.
setValue:forKey:@"foobaz").
I read in one of the e-mails from this list that if you use the
key-value coding method "setValue:forKey:", it will automatically call
the key-value observing methods "willChangeValueForKey:" and
"didChangeValueForKey:".
I see a problem here. If true, this means that if you change a property
via key-value coding methods, key-value observers will pick up the
change, but if you change a property via a mutator method, the
observers won't be notified. This means that if you want consistent
behavior, you always have to update properties via key-value coding
methods.
I suppose that one could write mutator methods that call
"willChangeValueForKey:" and "didChangeValueForKey:" manually, but then
that means redundant change notifications if one updates the property
via key-value coding.
Finally, maybe the key-value coding methods only call the k.v.o.
methods if the object returns YES from
"automaticallyNotifiesObserversForKey:".
So, to summarize my conclusions, either:
1. The e-mail was wrong and the k.v.c. methods do not automatically
call the k.v.o. methods.
2. The k.v.c. methods only call the k.v.o. methods if
"automaticallyNotifies..." returns YES.
3. The k.v.c. methods always call the k.v.o. methods.
and, finally,
4. If the k.v.c. methods call the k.v.o. methods, you can never use
mutators to update the properties, else k.v.o. won't work right.
I'm hoping conclusion #1 is correct, else #4 means that there will be
some rewritin' going on.
So what's the story?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.