Skip navigation.
 
mlRe: The magic of Key Value Coding and Key Value Observing
FROM : mmalc crawford
DATE : Fri Aug 31 07:28:54 2007

On Aug 30, 2007, at 9:29 PM, Erik Buck wrote:

> The underlying implementation of Key Value Observing
> uses "isa swizzling".  This is a clever trick enabled
> by the Objective-C runtime.
>

Well, this is an implementation detail to support "*automatic* KVO". 
All this does is insert invocations of {will,did}ChangeValueForKey 
{before,after} your set accessor method implementation to save you 
having to do so yourself.  If you want the "magic-free" version, then 
just implement manual notification as described in <http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/AutoVsManual.html
>.

> I just can't articulate why I am uncomfortable with KVC and KVO when 
> I am comfortable with connections in IB.
>

Being uncomfortable with KVC is a puzzler.  It's simply providing a 
further level of abstraction.  Would it help if you thought of it in 
terms of using the key to get a selector (NSSelectorFromString -- 
(@"key") for valueForKey: and (@"setKey:") for setValue:forKey:)[*] 
which is then used to invoke the corresponding accessor with an 
argument if appropriate?

I can understand a little more the discomfit KVO might give, since 
it's allowing a loose relationship between objects.  But again there's 
a venerable existing pattern in Cocoa that provides similar 
functionality -- notifications.  With notifications, you simply let 
anyone who's registered an interest know that what they're interested 
in has happened.  And if you're interested in an event, you ask the 
notification centre to let you know if/when it happens.  With KVO, 
it's much the same except that rather than asking a notification 
centre to mediate, you get in touch with the objects directly.  I'm 
sure you're comfortable enough with using notifications... I hope that 
might help?


> I look forward to language support for "properties."
> and hope that they will communicate intention in a way
> that I think is missing from current KVC and KVO.
>

The declared properties feature (as described at <http://developer.apple.com/leopard/overview/tools.html
>) is orthogonal to KVC.  It is possible to use KVC to access 
properties, and to use properties without ever using KVC.

A major design goal, however, is precisely to communicate intent (see 
for example the 'readonly' keyword).

mmalc


[*] Obviously it's a little more complicated than that, but if it 
helps you sleep at night... :-)

Related mailsAuthorDate
mlThe magic of Key Value Coding and Key Value Observing Erik Buck Aug 31, 06:29
mlRe: The magic of Key Value Coding and Key Value Observing mmalc crawford Aug 31, 07:28
mlRe: The magic of Key Value Coding and Key Value Observing mmalc crawford Aug 31, 07:40
mlRe: The magic of Key Value Coding and Key Value Observing Marcel Weiher Aug 31, 08:26