Re: Bind NSTableView to array of dictionaries in user defaults
Re: Bind NSTableView to array of dictionaries in user defaults
- Subject: Re: Bind NSTableView to array of dictionaries in user defaults
- From: Quincey Morris <email@hidden>
- Date: Thu, 05 May 2016 10:19:40 -0700
- Feedback-id: 167118m:167118agrif8a:167118shBRLrI8Y2:SMTPCORP
On May 5, 2016, at 06:44 , Jerry Krinock <email@hidden> wrote:
>
> I’m disapointed that the “magic of Cocoa Bindings” does not seem to provide a simple “code-less” solution to this simple requirement. Did I miss something?
It’s never been possible to bind “through” a NSArray or NSSet. AFAICT, that’s what’s gone wrong in your scenario, the involvement of user defaults here being a decorative factor only.
If you think about it, it should be clear that this “defect” is the same “How does my object know if any of its properties has changed?” question that comes up from time to time. There isn’t any good, straightforward way.
You can, for example, override ‘setValue:forKey:’ in the class of the array elements. That will give you a single point of intervention when any of the element properties are changed via KVC, as happens with bindings-related changes from the UI. But the element doesn’t automatically know what array it is a member of, so you’d need ad-hoc code to propagate the change to the specific property of the specific object that the array represents. (That is, it isn’t even enough to know the array.)
In this case, the simplest solution is probably to implement a custom setter (if Obj-C, or a ‘didSet’ accessor if Swift) for each property of each element that triggers a re-save of the user defaults. Alternatively, if what is at each index of the array is semantically fixed, you could save each element in user defaults under a unique key and avoid the array. Or, you might be able to use a NSDictionary instead of an array, which shouldn’t exhibit this “bind-through” limitation, because changes to NSMutableDictionary keys are 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