I'm trying to move a file even if a file with the same name already exists.
NSFileManager().moveItemAtURL(location1, toURL: location2)
Does NSFileManager
's method moveItemAtURL
have an override option? How can I replace the existing file?
You can always check if the file exists in the target location. if it does, delete it and move your item.
Swift 2.3
let filemgr = NSFileManager.defaultManager()
if !filemgr.fileExistsAtPath(location2)
{
do
{
try filemgr.moveItemAtURL(location1, toURL: location2)
}
catch
{
}
}
else
{
do
{
try filemgr.removeItemAtPath(location2)
try filemgr.moveItemAtURL(location1, toURL: location2)
}
catch
{
}
}
Swift 3+
try? FileManager.default.removeItem(at: location2)
try FileManager.default.copyItem(at: location1, to: location2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With