Re: Cocoa Binding with NSArrayController and NSMutableArray
Re: Cocoa Binding with NSArrayController and NSMutableArray
- Subject: Re: Cocoa Binding with NSArrayController and NSMutableArray
- From: Quincey Morris <email@hidden>
- Date: Mon, 3 Jan 2011 13:00:18 -0800
On Jan 3, 2011, at 11:01, Tilo Villwock wrote:
> In my XIB I've created a NSArrayController which is bound to my App Delegate and uses the name of the NSMutableArray variable as Model Key Path. I've also set my custom object class in the attributes section of the array controller.
Using an instance variable as a property works (KVC supports it by default) but is not recommended. (Properties are a kind of *behavior*, instance variables are a kind of *storage* used to back the property behavior when necessary.) Use a proper @property/@synthesize declaration for the property. Note also that the desired type for such an array property is typically NSArray* (though the underlying instance variable is NSMutableArray*). That's because exposing the mutability in the public property interface often leads to KVO compliance errors.
> I've got two columns in my table view which are connected to the array controller and use "selection" as the controller key and the corresponding variable names of the NSDate and NSNumber objects as Model Key Path.
No, use "arrangedObjects" as the controller key. "selection" refers to the object that's selected in the table view, which obviously not what you want in this case.
> Now, is there anything wrong with this setup? I've filled the array with 3 objects in "applicationDidFinishLaunching" so the table view should contain them at start since the binding uses KVO or am I completely wrong here?
You should fill the array KVO-compliantly, rather than using the simple 'addObject'-style methods on the instance variable. If the window containing the table view isn't being opened until after "applicationDidFinishLaunching", then either way will work, but if it's being opened during application launch, KVO compliance is necessary.
Also, if your window happens to be in your main menu NIB, make sure that "visible at launch" is *unchecked* in its IB properties. Leaving it checked can really mess things up. (There's really no reason why "visible at launch" should ever be checked, in any NIB. One time out of a thousand, having it checked might save you a line of code, but this almost never happens for non-toy applications.)
_______________________________________________
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