Instance Method


Replaces the contents of the item at the specified URL in a manner that ensures no data loss occurs.


- (BOOL)replaceItemAtURL:(NSURL *)originalItemURL withItemAtURL:(NSURL *)newItemURL backupItemName:(NSString *)backupItemName options:(NSFileManagerItemReplacementOptions)options resultingItemURL:(NSURL * _Nullable *)resultingURL error:(NSError * _Nullable *)error;



The item containing the content you want to replace.


The item containing the new content for originalItemURL. It is recommended that you put this item in a temporary directory as provided by the OS. If a temporary directory is not available, put this item in a uniquely named directory that is in the same directory as the original item.


If provided, the name used to create a backup of the original item.

The backup is automatically placed in the same directory as the original item. If an error occurs during the creation of the backup item, the operation fails. If there is already an item with the same name as the backup item, that item will be removed.

The backup item will be removed in the event of success unless the NSFileManagerItemReplacementWithoutDeletingBackupItem option is provided in options.


The options to use during the replacement. Typically, you pass NSFileManagerItemReplacementUsingNewMetadataOnly for this parameter, which uses only the metadata from the new item. You can also combine the options described in NSFileManagerItemReplacementOptions using the C-bitwise OR operator.


On input, a pointer for a URL object. When the item is replaced, this pointer is set to the URL of the new item. If no new file system object is required, the URL object in this parameter may be the same passed to the originalItemURL parameter. However, if a new file system object is required, the URL object may be different. For example, replacing an RTF document with an RTFD document requires the creation of a new file.


On input, a pointer to an error object. If an error occurs, this pointer is set to an error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value

YES if the replacement was successful or NO if an error occurred.


By default, the creation date, permissions, Finder label and color, and Spotlight comments of the original item are preserved on the new item. You can configure which metadata is preserved using the options parameter.

This method works only when the originalItemURL and newItemURL parameters are located on the same volume. Attempting to call this method by passing originalItemURL and newItemURL parameters that have locations on different volumes results in an error. Instead, you can call the URLForDirectory:inDomain:appropriateForURL:create:error: method, passing NSItemReplacementDirectory as the search path directory, to get a temporary URL on the destination's volume that is suitable for use with this method.

If an error occurs and the original item is not in the original location or a temporary location, the resulting error object contains a user info dictionary with the key "NSFileOriginalItemLocationKey". The value assigned to that key is an NSURL object with the location of the item. The error code is one of the file-related errors described in NSError Codes.

See Also

Replacing Items


Options for specifying the behavior of file replacement operations.