replaceItemAtURL :withItemAtURL:backupItemName:options:resultingItemURL:error:
Replaces the contents of the item at the specified URL in a manner that insures no data loss occurs.
- (BOOL)replaceItemAtURL:(NSURL *)originalItemURL withItemAtURL:(NSURL *)newItemURL backupItemName:(NSString *)backupItemNameoptions:(NSFileManagerItemReplacementOptions)optionsresultingItemURL:(NSURL **)resultingURL error:(NSError **)error
Parameters
- originalItemURL
- The item whose contents you want to replace.
- newItemURL
- 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.
- backupItemName
- Optional. If provided, this name is used to create a backup of the original item.The backup is placed in the same directory as the original item. If an error occurs during the creation of the backup item, the operation will fail. If there is already an item with the same name as the backup item, that item will be removed.[NSFileManager replaceItemAtURL]The backup item will be removed in the event of success unless the
NSFileManagerItemReplacementWithoutDeletingBackupItem
option is provided in options. - options
- Specifies the options to use during the replacement. Typically, you pass
0
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. - resultingURL
- 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 originalItemURLparameter. 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.[NSFileManager replaceItemAtURL]
- error
- On input, a pointer to an error object. If an error occurs, this pointer is set to an actual 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.Discussion of [NSFileManager replaceItemAtURL]
By default, the creation date, permissions, Finder label and color, and Spotlight comments of the original item will be preserved on the resulting item.
If an error occurs and the original item is not in the original location or a temporary location, the returned error object contains a user info dictionary with the
NSFileOriginalItemLocationKey
key. 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
.
Example of [NSFileManager replaceItemAtURL]
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:@"test.txt"];
// Create initial file in documents directory
if (![fileManager fileExistsAtPath:destinationPath])
{
BOOL fileCopied = [fileManager copyItemAtPath:sourcePath
toPath:destinationPath
error:&error];
if (!fileCopied)
[[self statusLabel] setText:[NSString stringWithFormat:@"Creation Error:\n\n%@", [error localizedDescription]]];
}
// Replace file in documents directory with copy of file from app bundle
if ([fileManager fileExistsAtPath:destinationPath])
{
// Create temporary file
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"test.txt"];
BOOL tempCopied = [fileManager copyItemAtPath:sourcePath
toPath:tempPath
error:&error];
if (!tempCopied)
[[self statusLabel] setText:[NSString stringWithFormat:@"Temp Creation Error:\n\n%@", [error localizedDescription]]];
// Replace file with temporary file
NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath];
BOOL fileReplaced = [fileManager replaceItemAtURL:destinationURL
withItemAtURL:[NSURL fileURLWithPath:tempPath]
backupItemName:nil
options:0
resultingItemURL:&destinationURL
error:&error];
if (!fileReplaced)
[[self statusLabel] setText:[NSString stringWithFormat:@"Replacement Error:\n\n%@", [error localizedDescription]]];
else
[[self statusLabel] setText:@"Successfully replaced file."];
}