问题
I try to copy my db from bundle path to destination path(document directory) and use this code in iOS8 , Swift3 and Xcode8 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)
}
}
But show me this error:
{Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
i have checked atPath and toPath that's right but i don't have any idea...
Please help me
Thank you
回答1:
Solved
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)
}
}
I added .path to my destination address and it works now.
来源:https://stackoverflow.com/questions/39743397/why-does-not-copy-database-db-from-bundle-to-document-directory-in-swift-3