Instance Method

writeToURL:ofType:forSaveOperation:originalContentsURL:error:

Writes the contents of the document to a file or file package located by a URL.

Declaration

- (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError * _Nullable *)outError;

Parameters

absoluteURL

The location to which the document contents are written.

typeName

The string that identifies the document type.

saveOperation

The type of save operation.

absoluteOriginalContentsURL

The location of the previously saved copy of the document (if not nil).

outError

On return, if the document contents could not be written, a pointer to an error object that encapsulates the reason they could not be written.

Return Value

YES if the document contents could be written; otherwise, NO.

Discussion

The default implementation of this method merely invokes [self writeToURL:absoluteURL ofType:typeName error:outError]. You can override this method instead of one of the three simple writing methods (writeToURL:ofType:error:,fileWrapperOfType:error:, and dataOfType:error:) if your document writing machinery needs access to the on-disk representation of the document revision that is about to be overwritten. The value of absoluteURL is often not the same as [self fileURL]. Other times it is not the same as the URL for the final save destination. Likewise, absoluteOriginalContentsURL is often not the same value as [self fileURL]. If absoluteOriginalContentsURL is nil, either the document has never been saved or the user deleted the document file since it was opened.

For backward binary compatibility with OS X v10.3 and earlier, if writeToFile:ofType:originalFile:saveOperation: is overridden and both URLs use the file: scheme, the default implementation of this method instead invokes:

[self writeToFile:[absoluteURL path]
      ofType:typeName
      originalFile:[absoluteOriginalContentsURL path]
      saveOperation:aSaveOperation];

The save operation used in this case is never NSAutosaveOperation; NSSaveToOperation is used instead.

See Also

Writing the Document's Content

- canAsynchronouslyWriteToURL:ofType:forSaveOperation:

Returns whether the receiver can concurrently write to a file or file package located by a URL, that is formatted for a specific type, for a specific kind of save operation.

- unblockUserInteraction

Unblocks the main thread during asynchronous saving.

- writeToURL:ofType:error:

Writes the contents of the document to a file or file package located by a URL, that is formatted to a specified type.

- writeSafelyToURL:ofType:forSaveOperation:error:

Writes the contents of the document to a file or file package located by a URL.

- fileWrapperOfType:error:

Creates and returns a file wrapper that contains the contents of the document, formatted to the specified type.

- dataOfType:error:

Creates and returns a data object that contains the contents of the document, formatted to a specified type.

- saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:

Saves the contents of the document to a file or file package located by a URL, that is formatted to a specified type, for a particular kind of save operation.

- saveToURL:ofType:forSaveOperation:completionHandler:

Saves the contents of the document to a file or file package located by a URL, that is formatted to a specified type, for a particular kind of save operation, and invokes the passed-in completion handler.

- fileAttributesToWriteToURL:ofType:forSaveOperation:originalContentsURL:error:

Returns the attributes to write to the file or file package at the specified URL, and targeting the specified type of save operation.

NSSaveOperationType

Constants for specifying the type of document-save operation to perform.