How to overwrite a file with NSFileManager when copying?

后端 未结 9 712
广开言路
广开言路 2020-12-02 14:13

I\'m using this method to copy a file:

[fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error];

I want to overwrite a fi

9条回答
  •  悲&欢浪女
    2020-12-02 14:45

    This is for improvement of 'Swift 3 and above' of question 'Move file and override [duplicate]' which is marked duplicate of this question.

    To move file from sourcepath(string) to DestinationPath(string). Delete the existing file if same name file already exists at DestinationPath.

    // Set the correct path in string in 'let' variables.
    let destinationStringPath = ""
    let sourceStringPath = ""
    
    let fileManager:FileManager = FileManager.default
    do
    {
        try fileManager.removeItem(atPath: sourceStringPath)
    }
    catch
    {
    }
    
    do
    {
        try fileManager.moveItem(atPath: sourceStringPath, toPath: destinationStringPath)
    }
    catch
    {
    }
    

提交回复
热议问题