移动文件并覆盖 [英] Move file and override

查看:144
本文介绍了移动文件并覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使文件已经存在,我仍在尝试移动文件.

I'm trying to move a file even if a file with the same name already exists.

NSFileManager().moveItemAtURL(location1, toURL: location2)

NSFileManager的方法moveItemAtURL是否具有覆盖选项?如何替换现有文件?

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
  {

  }
}

快速3 +

try? FileManager.default.removeItem(at: location2)
try FileManager.default.copyItem(at: location1, to: location2)

这篇关于移动文件并覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆