Document Based Application

  • My Document Based Application is to have n document types.

    Is it then proper to create n subclasses of NSDocument and the corresponding plist Document types entries, i.e. Item 0 thru Item n-1 ?

    Is it true that all n document types will have a common extension but will be identified by the Document types entires?

    -koko
  • On 08/07/2012, at 8:41 AM, koko wrote:

    > My Document Based Application is to have n document types.
    >
    > Is it then proper to create n subclasses of NSDocument and the corresponding plist Document types entries, i.e. Item 0 thru Item n-1 ?

    You can do it that way, or you can let a NSDocument subclass handle multiple types if they are quite similar, or a bit of both. i.e. it depends. In my app I have one document subclass for a number of filetypes, because they all end up as the same document type with the same internal data model.

    > Is it true that all n document types will have a common extension but will be identified by the Document types entires?

    If they all have the same extension then the system can't distinguish when a file is opened by your app what document object class it should instantiate, so that won't work.

    --Graham
  • I have 29 file types and wanted to get away from the if or switch to open them and let NSDocument pick the right class for me.

    As I understand it, an Item in the Document types array of the plist contains and entry for an NSDocument class.

    And yes, each type has a unique extension (possibly multiple).  I.e. type phobia has extensions pho, pho12, pho15 all mapped to NSDocument subclass MYPhobia.

    -koko

    On Jul 7, 2012, at 7:07 PM, Graham Cox wrote:

    >
    > On 08/07/2012, at 8:41 AM, koko wrote:
    >
    >> My Document Based Application is to have n document types.
    >>
    >> Is it then proper to create n subclasses of NSDocument and the corresponding plist Document types entries, i.e. Item 0 thru Item n-1 ?
    >
    >
    >
    > You can do it that way, or you can let a NSDocument subclass handle multiple types if they are quite similar, or a bit of both. i.e. it depends. In my app I have one document subclass for a number of filetypes, because they all end up as the same document type with the same internal data model.
    >
    >> Is it true that all n document types will have a common extension but will be identified by the Document types entires?
    >
    >
    > If they all have the same extension then the system can't distinguish when a file is opened by your app what document object class it should instantiate, so that won't work.
    >
    > --Graham
    >
    >
  • koko wrote:

    > I have 29 file types and wanted to get away from the if or switch
    > to open them and let NSDocument pick the right class for me.
    >
    > As I understand it, an Item in the Document types array of the
    > plist contains and entry for an NSDocument class.
    >
    > And yes, each type has a unique extension (possibly multiple).
    > I.e. type phobia has extensions pho, pho12, pho15 all mapped to
    > NSDocument subclass MYPhobia.

    Use an NSDictionary.  The key is the file extension.  The value is
    the subclass name, or the actual Class object.  Cost is one
    dictionary lookup.  Plus it's extensible, and can be revised without
    altering code.

    You can also add a level of indirection.  The key is still file
    extension, but the value is a number (int).  Use the number as an
    index into an NSArray of classname strings, or the actual Class objects.

    The dictionary and/or array can be stored as plists in your app's
    Resources sub-dir.

      -- GG
  • Quite nice, thanks GG.

    -koko

    On Jul 8, 2012, at 3:01 PM, Greg Guerin wrote:

    > koko wrote:
    >
    >> I have 29 file types and wanted to get away from the if or switch to open them and let NSDocument pick the right class for me.
    >>
    >> As I understand it, an Item in the Document types array of the plist contains and entry for an NSDocument class.
    >>
    >> And yes, each type has a unique extension (possibly multiple).  I.e. type phobia has extensions pho, pho12, pho15 all mapped to NSDocument subclass MYPhobia.
    >
    >
    > Use an NSDictionary.  The key is the file extension.  The value is the subclass name, or the actual Class object.  Cost is one dictionary lookup.  Plus it's extensible, and can be revised without altering code.
    >
    > You can also add a level of indirection.  The key is still file extension, but the value is a number (int).  Use the number as an index into an NSArray of classname strings, or the actual Class objects.
    >
    > The dictionary and/or array can be stored as plists in your app's Resources sub-dir.
    >
    > -- GG
    >
previous month july 2012 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 31          
Go to today