即使同名文件已存在,我也尝试移动文件。
NSFileManager().moveItemAtURL(location1, toURL: location2)
Does NSFileManager
的方法moveItemAtURL
有覆盖选项吗?如何替换现有文件?
您始终可以检查目标位置中是否存在该文件。
如果是,请将其删除并移动您的项目。
斯威夫特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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)