为什么不在swift 3中将database.db从bundle复制到document目录?

我尝试将我的数据库从bundle路径复制到目标路径(文档目录),并在iOS8Swift3Xcode8 final中使用此代码:

let bundlePath = Bundle.main.path(forResource: "cry", ofType: ".db") print(bundlePath, "\n") //prints the correct path let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let fileManager = FileManager.default let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent("cry.db") let fullDestPathString = String(describing: fullDestPath) if fileManager.fileExists(atPath: fullDestPathString){ print("Database file is exist") print(fileManager.fileExists(atPath: bundlePath!)) }else{ do{ try fileManager.copyItem(atPath: bundlePath!, toPath: fullDestPathString) }catch{ print("\n") print(error) } } 

但是告诉我这个错误

 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 

我检查了atPathtoPath是对的,但我不知道……
请帮帮我
谢谢

解决了

  let bundlePath = Bundle.main.path(forResource: "cry", ofType: ".db") let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let fileManager = FileManager.default let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent("cry.db") if fileManager.fileExists(atPath: fullDestPath.path){ print("Database file is exist") print(fileManager.fileExists(atPath: bundlePath!)) }else{ do{ try fileManager.copyItem(atPath: bundlePath!, toPath: fullDestPath.path) }catch{ print("\n",error) } } 

我将.path添加到我的目的地地址,现在可以使用了。