objective-ccocoansuserdefaultsnspredicateeditor

Binding NSPredicateEditor to Shared User Defaults Controller


I have an NSPredicateEditor that is part of a preference panel for a tool that I am making.

I would like to store the user's selections just as I would any other item in the NSUserDefaults object, but when I add the Binding to the NSPredicateEditor object and run the tool, it will not keep data and further it causes the other defaults to not show. When I take away the binding and recompile, everything else comes back.

What is the best way to store the NSPredicatedEditor data as a user preference?

Thanks.


Solution

  • Whenever the UI of an NSPredicateEditor changes, those changes are immediately reflected in the editor's objectValue (ie, its represented NSPredicate). So you can just grab that predicate and archive it using an NSKeyedArchiver. This will effectively turn the predicate into an NSData object, which can be saved out to NSUserDefaults.

    To read it back in, just grab the NSData object, run it through an NSKeyedUnarchiver, and setObjectValue: on your predicate editor.

    You can probably accomplish this through bindings, though I have never tried it. You can bind the value of an NSPredicateEditor to something that supplies an NSPredicate (or an NSPredicate in NSData form). You'll also probably need to specify that the value transformer is the NSKeyedUnarchiveFromData transformer.