How to overwrite a file with NSFileManager when copying?

后端 未结 9 697
广开言路
广开言路 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:55

    If you're not sure if the file exists, this works on swift 3+

    try? FileManager.default.removeItem(at: item_destination)
    try FileManager.default.copyItem(at: item, to: item_destination)
    

    The first line fails and is ignored if the file doesn't already exist. If there's a exception durning the second line, it throws as it should.

提交回复
热议问题