• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Setting values by key vs. by method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting values by key vs. by method


  • Subject: Re: Setting values by key vs. by method
  • From: Scott Anguish <email@hidden>
  • Date: Sat, 8 Nov 2003 18:32:38 -0500

On Nov 8, 2003, at 5:24 PM, Dustin Voss wrote:

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.

No, the accessors also notice the changes, and call the appropriate KVO 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.


IF automataticallyNOtifies is YES, then both KVC and accessors cause KVO notifications automatically
if it's no, then neither do it automatically (you'd need to implement the did/will change methods in your accessors).

I'm hoping conclusion #1 is correct, else #4 means that there will be some rewritin' going on.


1 is wrong, 2 is correct, with the stipulation that accesssors also call the kvo methods. 3 is wrong, 4 is way wrong.

Aside from claiming that directly manipulating the instance variable also calls the kvo methods, the docs are correct.
_______________________________________________
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.
References: 
 >Setting values by key vs. by method (From: Dustin Voss <email@hidden>)

  • Prev by Date: Re: Key-value observing and NSMutableArray
  • Next by Date: Code Example from WWDC
  • Previous by thread: Setting values by key vs. by method
  • Next by thread: Code Example from WWDC
  • Index(es):
    • Date
    • Thread