My app’s document consists of a tree of individual word processing files to be 
saved as separate files within an NSFileWrapper.

Apple’s documentation suggests you can make the process of saving to a file 
wrapper more efficient if you only write out new or changed files. I’d like to 
do that and avoid needlessly rewriting subdocuments which haven’t changed.

The example Apple gives us in listing 6-5 of the Document-Based App Programming 
Guide demonstrates how to write data to a new file wrapper when the data 
doesn’t already exist (note that the calls to [fileWrappers objectForKey:] are 
expected to return nil). What the docs don’t demonstrate and I’m having 
difficulty figuring out is, how do you write changed data if that call doesn’t 
return nil?

Here’s the helper method I’m working on, including a comment showing where I 
don’t know what to do:

  func saveToWrapper(  
    #parentWrapper: NSFileWrapper,
    err: NSErrorPointer
    )
  {
    var fw = parentWrapper.fileWrappers[ filename ] as? NSFileWrapper
    if ( contentWasModified || fw == nil ) {
      var attr = [ NSDocumentTypeDocumentAttribute : NSRTFTextDocumentType ]
      var range = NSRange( location: 0, length: content.length )
      var data = content.dataFromRange(
        range,
        documentAttributes: attr,
        error: err
      )
      if ( fw == nil ) {
        parentWrapper.addRegularFileWithContents( data, preferredFilename: 
filename )
      } else {
        // What the heck do I do here? How to I update fw's contents?
      }
    }
 }


This is an instance method of a class which represents the subdocument. The 
class contains the instance variables filename: String, content: 
NSAttributedString, and contentWasModified: Bool.

—

Charles Jenkins

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to