On 11 Nov 2005, at 1:28, Rod Schmidt wrote:
> I have some questions about RubyCocoa's support for key value
> coding and the new kvc_accessor and kvc_depends_on methods.
>
> First off, in Objective-C, all objects are already KVC compliant as
> long as you follow certain naming conventions. So why do we have a
> separate kvc_accessor method instead of just modifiying
> attr_accessor to support key value coding? Is it because
> attr_accessor is built into Ruby and we needed a different name?
KVC in ObjC works by wrapping setter methods with
willChangeValue:forKey: didChangeValue:forKey:. It does this by some
sneaky overriding of the method pointers in the class definition.
The RubyCocoa classes are dynamically generated, and at the moment
ObjC methods do not actually exist that correspond to the getter/
setter. Instead, we catch valueForUndefinedKey: and
setValue:forUndefinedKey: and pass through to the ruby getter/setter.
At this stage, the willChange/didChange stuff is done explicitly.
All this is necessary because KVC can't step in and do its sneaky
thing because the methods do not exist in objc land.
The same thing situation is there for most methods called from objc -
the objc forwardInvocation: method is used to pass unknown selectors
to ruby.
Just in the past week on this mailing list we've been discussing a
problem that arises with CoreData, where it requires the real methods
to exist in objc land. What I'm not sure of yet is whether creating
these methods in the dynamically generated class allows KVC to do the
magic on our behalf, or whether it's still necessary to do the
willChange/didChange notifications.
> Next, what does kvc_depends_on do? I'm guessing it's for Key Value
> Observing? If that's the case why don't we just use the addObserver
> method in NSObject that is for that purpose? Or is it just a shortcut?
kvc_depends_on is just a wrapper for
setKeys:triggerChangeNotificationsForDependentKey:, making it a bit
more ruby-like. Here's the definition:
def kvc_depends_on(keys, *dependencies)
dependencies.flatten.each do |dependentKey|
setKeys_triggerChangeNotificationsForDependentKey(keys.to_a,
dependentKe
y)
end
end
> Sorry if these questions have been answered before, but I haven't
> seen a description of what these do. Just scattered discussions
> with no clear definition.
The binding support stuff is new. I know if there's any particular
documentation for it, other than the discussion on the mailing lists.
Cheers,
Jonathan
|