Re: Binding : NSArrayController does not reflect changes to NSMutableArray
Re: Binding : NSArrayController does not reflect changes to NSMutableArray
- Subject: Re: Binding : NSArrayController does not reflect changes to NSMutableArray
- From: Ken Thomases <email@hidden>
- Date: Fri, 28 Nov 2008 01:44:33 -0600
On Nov 27, 2008, at 2:12 AM, Mike Chambers wrote:
First, I am new to Cocoa, so I apologize if this question is super
simple.
No, it's something that bites a lot of people.
I am currently reading through "Cocoa Programming for Max OS X", and
am trying to create a simple example of a NSTableView that is bound to
an NSArrayController which is bound to an NSMutableArray in my
application controller class.
Just as a pedantic point of clarification: you are not binding
anything to an NSMutableArray. You are binding to a _property_ of
your controller object. That property is represented by the methods
in your controller class's interface. It so happens that, with the
way you've chosen to implement your controller class, the property is
backed by an NSMutableArray.
This is an important distinction and once you grok it, KVO and
bindings will make more sense.
However, when I change (i.e. add) data in the NSMutableArray the
changes are not reflected in the NSArrayController. If I modify the
NSArrayController directly, I can see the changes in the NSTableView
After a lot of searching, I found this post:
http://chanson.livejournal.com/85659.html
which basically says changes to NSMutableArray will not automatically
be picked up by the NSArrayController.
That's because KVO and bindings are hooked into your controller object
and its methods. They are not hooked into the NSMutableArray at all!
So, only mutations of the property done through certain specific
methods of your controller object will be noticed.
However, even If I implement
the solutions in that post, it is still not working for me.
That post led you a bit astray with a simple typographical error. The
methods should have "in" in their names, not "into".
See the Apple docs that that post referenced: http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/AccessorConventions.html#/
/apple_ref/doc/uid/20002174-178830-BAJEDEFB
Also see the docs for -mutableValueForKey:. Actually, better is to
read the comments at the declaration for that method in
NSKeyValueCoding.h, because it has some details which the docs are
(still!) missing.
Besides the insertObject:in<Key>AtIndex: method, you may need to
implement the corresponding "remove" method, too. Even if you don't
have to, you should.
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