我遇到了这个令人沮丧的问题,我希望有人可以帮助我。 我尝试使用fileManager.copyItem(atPath,toPath)将项目从一个文件夹复制到另一个文件夹。我确定目标文件夹是空的,但每次我尝试复制时都会收到此错误:
哎呀!出错了:Error Domain = NSCocoaErrorDomain Code = 516“”myFileName“无法复制到”Desktop“,因为已经存在同名的项目。” UserInfo = {NSSourceFilePathErrorKey = / Users / Antan / Desktop / 1 / myFileName,NSUserStringVariant =( 复制 ),NSDestinationFilePath = / Users / Antan / Desktop / 2,NSFilePath = / Users / Antan / Desktop / 1 / myFileName,NSUnderlyingError = 0x60000045ac40 {Error Domain = NSPOSIXErrorDomain Code = 17“File exists”}}
代码:
let fileManager = FileManager.default
do {
try fileManager.copyItem(atPath: "/Users/Antan/Desktop/1/myFileName", toPath: "/Users/Antan/Desktop/2")
} catch let error as NSError {
print("Ooops! Something went wrong: \(error)")
}