Re: NSTreeController Content Array as index accessor object -- what is the <Key>?
Re: NSTreeController Content Array as index accessor object -- what is the <Key>?
- Subject: Re: NSTreeController Content Array as index accessor object -- what is the <Key>?
- From: Quincey Morris <email@hidden>
- Date: Sat, 21 Feb 2009 17:25:13 -0800
On Feb 21, 2009, at 15:24, Stuart Malin wrote:
I'm confused using an NSTreeController. I have a node object that
implements indexed accessors. This works. For I have an OutlineView
that is bound to a nib instantiated instance of TreeController. My
problem is this:
First, I bound the TreeController's Content Array to an instance of
MSMutableArra in my AppController (setting in IB the AppController
as the "bind to" and setting the Model Key Path to the name of the
mutable array ivar. This works partly -- the Outline View doesn't
display new nodes that I add to the mutable array.
I think to myself: but of course: NSMutableArray is not KVO-
compliant (right?)
So I constructed a class that has a mutable array as a backing
store, and expose the KVC indexed accessors for a to-many object.
If I understand your scenario correctly, this approach isn't going to
help. All you end up with is a different array implementation, which
is no more "KVO-compliant" than the original NSMutableArray.
Presently, when I run, an exception is generated:
Cannot create NSArray from object <GRIndexArray: 0x1417b0> of class
GRIndexArray
Is GRIndexArray a subclass of NSMutableArray? In this case, it
probably has to be, because the NSTreeController expects it to be. (In
other circumstances, when you're accessing a pseudo-array only from
your own code, it's often fine if it just behaves like an array
without actually being a NS[Mutable]Array. But, as I said above, I
don't think this is anything to do with your real problem.
The Cocoa Bindings Programming Topic says:
"The contentArray binding is bound to an NSArray or an object that
implements the appropriate array indexed accessor methods."
But... I'm not sure what <Key> to use. How would NSTreeController
know this? Where in IB do I set? Or might this be derived from the
class name?
As you can see, I'm quite confused.
My apologies if this question should be answered in the docs, which
I am consulting, but am just not seeing my way.
The real issue is probably that you're not modifying your array
property (e.g. when adding new nodes) KVO-compliantly. Even though an
array object is not "KVO-compliant", an array property of a model
object *can* be modified KVO-compliantly.
For example, if you have a MyController object with a myNodes property
backed by a myNodesArray instance variable, then this:
[myNodesArray insertObject: someObject atIndex: someIndex];
won't work, because arrays are not KVO-compliant for insertions,
deletions or removals. But this:
[[myController mutableArrayValueForKey: @"myNodes"] insertObject:
someObject atIndex: someIndex];
*will* work, because myController is KVO-compliant for the key myNodes
when modified this way.
Note that you could (alternatively) implement the accessors
insertObject:inMyNodesAtIndex: and removeObjectFromMyNodesAtIndex: *in
MyController*. In that case, you can call the accessors directly, and
that also is KVO-compliant.
_______________________________________________
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