0
votes

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];
  }
1
When you say you've read the docs, which ones? I'd argue the Xcode project template is very clear how to implement saving etc. - Mike Abdullah
Your code makes no sense. Why are you checking the error pointer before doing anything else? - Mike Abdullah

1 Answers

1
votes

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.