本文介绍了为什么不把database.db从bundle到swift 3中的document目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将我的数据库从包路径复制到目标路径(文档目录),并在 iOS8 , Swift3 和 Xcode8 final :
let bundlePath = Bundle.main.path(forResource:cry,ofType:.db)
print(bundlePath,\\\
)//打印正确的路径
let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true).first!
let fileManager = FileManager.default
let fullDestPath = URL(fileURLWithPath:destPath).appendingPathComponent(cry.db)
let fullDestPathString = String(describe:fullDestPath)
if fileManager.fileExists(atPath:fullDestPathString){
print(Database file is exists)
print(fileManager.fileExists(atPath:bundlePath!))
} else {
do {
try fileManager.copyItem(atPath:bundlePath !, toPath:fullDestPathString)
} catch {
print(\\\
)
print(error)
}
}
但显示此错误 p>
{Error Domain = NSPOSIXErrorDomain Code = 2没有这样的文件或目录}}
我已经选中了 atPath 和 toPath ,但我没有任何想法。 。
请帮助我
谢谢解决方案/ strong>
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 exists)
print(fileManager.fileExists(atPath:bundlePath!))
} else {
do {
try fileManager.copyItem :bundlePath !, toPath:fullDestPath.path)
} catch {
print(\\\
,error)
}
}
我已将 .path 添加到我的目标地址,现在可以正常工作。
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解决方案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.
这篇关于为什么不把database.db从bundle到swift 3中的document目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!