• 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
Setting values by key vs. by method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: Setting values by key vs. by method
      • From: Scott Anguish <email@hidden>
  • Prev by Date: Re: NSUserDefaultsController
  • Next by Date: Re: Palm Desktop User Data
  • Previous by thread: Re: Key-value observing and NSMutableArray
  • Next by thread: Re: Setting values by key vs. by method
  • Index(es):
    • Date
    • Thread