NSUserDefaults per Document

  • Hello,

      I have a set of preferences that display in a sheet on a document
    window.  The preferences are saved /for/ the document on that
    machine, but not /in/ the document itself.  Each of my documents
    contains a UUID I can to use to key the preferences for that document.

      I'd like to use all the goodness of NSUserDefaults and IB bindings
    to set the controls on the sheet.  Perhaps with some sort of
    dictionary in the NSUserDefaults for each document?  How do I specify
    this dictionary as the defaults for the sheet?

      I've read the archives, Apple's documentation and I don't quite see
    how to do this.

    Thank you,
      Sanford
  • Am 29.09.2007 um 17:34 schrieb Sanford Selznick:

    > Hello,
    >
    > I have a set of preferences that display in a sheet on a document
    > window.  The preferences are saved /for/ the document on that
    > machine, but not /in/ the document itself.  Each of my documents
    > contains a UUID I can to use to key the preferences for that document.
    >
    > I'd like to use all the goodness of NSUserDefaults and IB
    > bindings to set the controls on the sheet.  Perhaps with some sort
    > of dictionary in the NSUserDefaults for each document?  How do I
    > specify this dictionary as the defaults for the sheet?
    >
    > I've read the archives, Apple's documentation and I don't quite
    > see how to do this.

    You could implement a getter and setter for the prefs dictionary in
    your document controller (via NSUserDefaults, retrieving the
    dictionary for the document GUID). Then, instead of binding to the
    shared user defaults, bind to your document controller with the path
    prefixed with your getter method, e.g.

    @interface MyDocumentController {
    }

    - (id)documentPrefs;
    - (void)setDocumentPrefs:(id)prefs;

    @end

    @implementation MyDocumentController
    - (id)documentPrefs
    {
    return [[NSUserDefaults standardUserDefaults] objectForKey:[self
    documentID]];
    }
    - (void)setDocumentPrefs:(id)prefs
    {
    return [[NSUserDefaults standardUserDefaults] setObject:prefs forKey:
    [self documentID]];
    }
    @end

    then bind to your document controller with path "documentPrefs.<key>".

    Not a "pure" bindings implementation as you need to write that little
    glue code that will get and set the prefs for a given document, but
    still only about 2 real lines of code ;-)

    HTH,
    </jum>
  • On 9/29/07 8:34 AM, Sanford Selznick said:

    > I have a set of preferences that display in a sheet on a document
    > window.  The preferences are saved /for/ the document on that
    > machine, but not /in/ the document itself.

    I'm curious why?  If you take this approach, these prefs will be lost
    when the document is emailed/shared between computers/users.

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • At 12:30 PM -0400 10/2/07, Sean McBride wrote:
    > On 9/29/07 8:34 AM, Sanford Selznick said:
    >
    >> I have a set of preferences that display in a sheet on a document
    >> window.  The preferences are saved /for/ the document on that
    >> machine, but not /in/ the document itself.
    >
    > I'm curious why?  If you take this approach, these prefs will be lost
    > when the document is emailed/shared between computers/users.

    Because my documents have preferences that are specific to a single account.

    Simple example: Text editors save window size and location for a
    given document on a specific machine.  So when the user moves the
    document to another machine with a different sized monitor, it fits
    consistently.  And a text file isn't a plist so there's no place else
    to store this information.

    Best,
      Sanford
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today