NSDocument custom file Package or URL override?

  • My document format needs to look like:

    File (actually a directory marked as a package)
    -- MyData.plist
    -> Images (directory)
    ----> Image1.png
    ----> Image2.png
    -> Data (directory)
    ----> Data1.dat
    ----> Data2.dat
    -> Icons (directory)
    ----> Icon1.png
    ----> Icon1.png

    In my NSDocument subclass, Overriding:

    - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper
    ofType:(NSString *)typeName error:(NSError **)outError

    Doesn't seem to be the right thing to do since it seems to want to put all
    the files in a single flat directory within the package

    Overriding:

    -(BOOL)readFromURL:(NSURL *)inAbsoluteURL
    ofType:(NSString *)inTypeName error:(NSError **)outError

    This seems a bit better but seems designed to read from a single file... Can
    that file be my directory/package.

    What is the best way to do this?
  • On 5/15/13 5:14 PM, Trygve Inda wrote:
    > My document format needs to look like:
    >
    > File (actually a directory marked as a package)
    > -- MyData.plist
    > -> Images (directory)
    > ----> Image1.png
    > ----> Image2.png
    > -> Data (directory)
    > ----> Data1.dat
    > ----> Data2.dat
    > -> Icons (directory)
    > ----> Icon1.png
    > ----> Icon1.png
    >
    >
    > In my NSDocument subclass, Overriding:
    >
    > - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper
    > ofType:(NSString *)typeName error:(NSError **)outError
    >
    > Doesn't seem to be the right thing to do since it seems to want to put all
    > the files in a single flat directory within the package

    It's the right place. You'll get a filewrapper representing the document-package
    directory (the root). From there, you can query the root wrapper for
    sub-wrappers using -fileWrappers (use -isDirectory and -isRegularFile to find
    out if a wrapper contains other wrappers).

    For writing the document, you'll overwrite -fileWrapperOfType:error: and return
    a root wrapper that contains your entire package structure. You can construct
    the interior of your package format to your liking, you can also add directories
    by adding a file wrapper that represents a directory - created via
    -initDirectoryWithFileWrappers:

    The NSDocument guide contains a section about using wrappers and demos the basics:

    http://developer.apple.com/library/mac/#documentation/DataManagement/Concep
    tual/DocBasedAppProgrammingGuideForOSX/AdvancedTopics/AdvancedTopics.html


    Regards
    Markus
    --
    __________________________________________
    Markus Spoettl
  • > On 5/15/13 5:14 PM, Trygve Inda wrote:
    >> My document format needs to look like:
    >>
    >> File (actually a directory marked as a package)
    >> -- MyData.plist
    >> -> Images (directory)
    >> ----> Image1.png
    >> ----> Image2.png
    >> -> Data (directory)
    >> ----> Data1.dat
    >> ----> Data2.dat
    >> -> Icons (directory)
    >> ----> Icon1.png
    >> ----> Icon1.png
    >>
    >>
    >> In my NSDocument subclass, Overriding:
    >>
    >> - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper
    >> ofType:(NSString *)typeName error:(NSError **)outError
    >>
    >> Doesn't seem to be the right thing to do since it seems to want to put all
    >> the files in a single flat directory within the package
    >
    > It's the right place. You'll get a filewrapper representing the
    > document-package
    > directory (the root). From there, you can query the root wrapper for
    > sub-wrappers using -fileWrappers (use -isDirectory and -isRegularFile to find
    > out if a wrapper contains other wrappers).
    >
    > For writing the document, you'll overwrite -fileWrapperOfType:error: and
    > return
    > a root wrapper that contains your entire package structure. You can construct
    > the interior of your package format to your liking, you can also add
    > directories
    > by adding a file wrapper that represents a directory - created via
    > -initDirectoryWithFileWrappers:
    >
    > The NSDocument guide contains a section about using wrappers and demos the
    > basics:
    >
    > http://developer.apple.com/library/mac/#documentation/DataManagement/Concep
    tua

    > l/DocBasedAppProgrammingGuideForOSX/AdvancedTopics/AdvancedTopics.html
    >
    > Regards
    > Markus

    One issue is that when opening my document (the package may contain several
    thousand files) I only really need to load one file (the plist) and then
    there rest of the files are images that are only read as needed... So I will
    need to have my doc keep a reference to the package location to read these
    images outside of the normal save/read methods.

    In looking at the docs:

    - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName
    error:(NSError **)outError

    it says " Sets the contents of this document by reading from a file or file
    package, of a specified type, located by a URL."

    So this seems to work for document packages too??
  • On 5/17/13 8:11 AM, Trygve Inda wrote:
    > One issue is that when opening my document (the package may contain several
    > thousand files) I only really need to load one file (the plist) and then
    > there rest of the files are images that are only read as needed... So I will
    > need to have my doc keep a reference to the package location to read these
    > images outside of the normal save/read methods.
    >
    > In looking at the docs:
    >
    > - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName
    > error:(NSError **)outError
    >
    > it says " Sets the contents of this document by reading from a file or file
    > package, of a specified type, located by a URL."
    >
    > So this seems to work for document packages too??

    Yes, sure, but:

    An alternative is to hold on to the packages sub-wrappers and read their content
    on demand only. No one forces you to load the wrappers' contents at the time
    your document is opened, so you would only actually load your plist and keep the
    other wrappers around.

    That also gives you to possibility to reuse the wrappers when you need to save
    your document. When you reuse an "old" wrapper (say because the content of the
    file the wrapper represents didn't change), it dramatically shortens the time
    needed to save the document - if you have many files to save and/or if they are
    big. That's because the content won't be written twice, the system just creates
    a hard link to the original file on the disk.

    Regards
    Markus
    --
    __________________________________________
    Markus Spoettl
previous month may 2013 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