Re: Bindings Concept Question
Re: Bindings Concept Question
- Subject: Re: Bindings Concept Question
- From: Ken Thomases <email@hidden>
- Date: Sat, 21 Jun 2008 01:15:42 -0500
On Jun 21, 2008, at 12:16 AM, Alex Wait wrote:
When the app starts, I see the data as expected. However, when I
call my add
function, which gets the strings from the textFields and makes the
new "Data" object and adds it to the array, I do not see it in the
table.
Without seeing your add method, I can't be sure but I can guess at the
problem. I suspect you're modifying the mutable array in a manner
which does not send out the proper KVO (key-value observing)
notifications.
I'll quote myself from an earlier thread <http://lists.apple.com/archives/cocoa-dev/2008/May/msg01031.html
>:
There are two common misconceptions [...]:
1) KVO is not for observing properties, as such. It's for observing
the object which _has_ the property. To put it another way: I don't
observe the number, I observe the controller for changes in its
number property.
2) A property is _not_ the ivar. The ivar, if it exists at all, is
an implementation detail of how the class implements the property.
A property is part of the interface of the object. You will find it
much easier if you conceptualize a property as the set of KVC-
conforming methods in the object's interface. A "key" is a string
naming or identifying a property. (Yes, KVC and KVO provide built-
in support for properties which don't have accessor methods. They
will access the ivar directly. However, this is just a convenience
and a fall-back position. It doesn't materially change how you
should conceptualize properties.)
To illustrate:
@interface Foo : NSObject
{
NSNumber* number; // <-- This is NOT the property
}
// _These_ are the property:
- (NSNumber*) number;
- (void) setNumber:(NSNumber*)newNumber;
@end
// ... in some other code somewhere
Foo* myFoo = /* ... */
[myFoo addObserver:self forKeyPath:@"number"];
This object (self) is observing the myFoo object for changes in its
"number" property.
[...] Often people have a class with an NSMutableArray ivar, and
they get confused as to why modifications that they make directly to
that ivar (as by -addObject:, for example) don't result in KVO
notifications and updates to the bound GUI elements. The reason is
that nothing is observing that array. They are observing the owning
object for changes of the property for which that ivar is backing
storage.
The array does not (and can not) send out KVO notifications. For
one thing, it doesn't know what object owns it nor what property it
represents. The owning object is what sends out the KVO
notifications. In order for it to do that, the owning object must
be messaged. It might be messaged with a KVC-conforming setter such
as set<Key>: or insertObject:in<Key>AtIndex:, in which case KVO
hooks into those methods using low-level techniques of the Objective-
C runtime (isa-swizzling). Or, it might be messaged with will/
didChange:valuesAtIndexes:forKey:. One way or another, though, the
owning object has to be messaged if it is to produce KVO
notifications.
KVO does provide the -mutableArrayValueForKey... methods to create a
proxy object which you can treat as a mutable array but which
messages the owning object to carry out the actual modifications.
In that way, the owning object is able to send out KVO notifications
as necessary. (Even if the proxy ends up accessing the ivar
directly, it still messages the owning object with -will/
didChange:valuesAtIndexes:forKey:.)
I hope that makes it clearer.
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden