移动文件并覆盖
我正在尝试移动文件,即使已经存在具有相同名称的文件。
NSFileManager().moveItemAtURL(location1, toURL: location2)
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 { } }