3
即使存在具有相同名称的文件,也要移动文件。移动文件并覆盖
NSFileManager().moveItemAtURL(location1, toURL: location2)
不NSFileManager
的方法moveItemAtURL
有一个覆盖选项?我怎样才能替换现有的文件?
即使存在具有相同名称的文件,也要移动文件。移动文件并覆盖
NSFileManager().moveItemAtURL(location1, toURL: location2)
不NSFileManager
的方法moveItemAtURL
有一个覆盖选项?我怎样才能替换现有的文件?
您可以随时检查文件是否存在于目标位置。 如果有,请将其删除并移动您的物品。
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
{
}
}
这样岂不更简洁:'如果filemgr.fileExistsAtPath(LOCATION2) { 做 { 试filemgr.removeItemAtPath(LOCATION2) } 抓 {} } 做 { 尝试filemgr.moveItemAtURL(LOCATION1,的toURL:LOCATION2) } ç atch { }' – KaraBenNemsi
删除目标文件,然后进行移动。 – EmilioPelaez
很多objc都回答了这个问题。您可能可以将这些答案中的一部分转换为swift,例如http://stackoverflow.com/questions/6137423/how-to-overwrite-a-file-with-nsfilemanager-when-copying和http:// stackoverflow .COM /问题/ 20683696 /如何到覆盖 - 一个用文件夹 - - 的NSFileManager-defaultmanager-,复印时 –