Why does not copy database.db from bundle to document directory in swift 3? [duplicate]

谁说我不能喝 提交于 2019-12-08 02:15:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!