• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Updating Content of NSFileWrapper
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Updating Content of NSFileWrapper


  • Subject: Updating Content of NSFileWrapper
  • From: Charles Jenkins <email@hidden>
  • Date: Mon, 15 Sep 2014 22:12:17 -0400

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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Updating Content of NSFileWrapper
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: Nib unloading and ARC
  • Next by Date: Re: Updating Content of NSFileWrapper
  • Previous by thread: Re: Nib unloading and ARC
  • Next by thread: Re: Updating Content of NSFileWrapper
  • Index(es):
    • Date
    • Thread