Cory D. Wiles
Cory D. Wiles

Reputation: 556

Best Practices For Saving NSDocument

I am working on a basic hello world application that will open a txt document, edit it and then finally save the document. What is the best practice for handling the edit.

I have read Apple's documentation throughly, but when I check for the NSError **outError the method returns NO. When I comment it out - (BOOL)writeToURL:(NSURL*)writeURL ofType:(NSString*)type forSaveOperation:(NSSaveOperationType)saveOp originalContentsURL:(NSURL*)origURL error:(NSError**)errorPtr will write out the file.

Is there a best practice for saving a document? Is there something wrong with the below implementation? If you comment out the check for errorPtr then the modify file is written to original file.

 - (BOOL)writeToURL:(NSURL*)writeURL 
             ofType:(NSString*)type 
   forSaveOperation:(NSSaveOperationType)saveOp 
originalContentsURL:(NSURL*)origURL 
              error:(NSError**)errorPtr {

  if (errorPtr) {

    *errorPtr = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];

    return NO;
  }


    return [[[self.txtView textStorage] string] writeToURL:writeURL atomically:NO encoding:NSUTF8StringEncoding error:errorPtr];
  }

Upvotes: 0

Views: 455

Answers (1)

Yoav
Yoav

Reputation: 6098

Override fileWrapperOfType:error: or dataOfType:error: instead.

And also, the error pointer is an out parameter. You should set it inside your function if needed and if it's not null.

Upvotes: 1

Related Questions