writeContents(_:andAttributes:safelyTo:for:)
Ensures that document data is written safely to a specified location in the application sandbox.
Declaration
func writeContents(_ contents: Any, andAttributes additionalFileAttributes: [AnyHashable : Any]? = nil, safelyTo url: URL, for saveOperation: UIDocument.SaveOperation) throwsParameters
- contents:
The document data to write to disk. Typically, the data is encapsulated by an Nsdata object (if a flat file) or an Filewrapper object (if a file package).
If the object encapsulating the document data is of some other type, you should override this method or Writecontents(_:to:for:originalcontentsurl:) to perform the actual writing of the data.
- additionalFileAttributes:
A dictionary of Filemanager file attributes to assign to the document file. The default implementation obtains these file attributes by calling Fileattributestowrite(to:for:).
- url:
The file URL specifying the location of the document file in the application sandbox.
- saveOperation:
A constant that indicates whether the document file is being written the first time or whether it is being overwritten. See Saveoperation for details.
Discussion
This method is called by the save(to:for:completionHandler:) method to save the file data (and associated attributes in the case of an FileWrapper). It creates temporary files and directories as necessary so that successful saves can be completed atomically and failed saves can be rolled back cleanly. This method calls writeContents(_:to:for:originalContentsURL:) to save the contents object, passing the location for the new saved file in the toURL parameter and the location of the previously existing file in the originalContentsURL parameter, if this is an overwrite operation.
If you want to change how file data is saved, you generally override the writeContents(_:to:for:originalContentsURL:) method instead of this method. Additionally, you don’t need to call this method directly unless you are overriding the save(to:for:completionHandler:) method.