rez*_*afi 2 database copy ios swift
我尝试将我的数据库从bundle路径复制到目标路径(文档目录),并在iOS8,Swift3和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)
}
}
Run Code Online (Sandbox Code Playgroud)
但是告诉我这个错误:
{Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Run Code Online (Sandbox Code Playgroud)
我检查过atPath和toPath是对的,但我不知道......
请帮帮我
谢谢
解决了
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)
}
}
Run Code Online (Sandbox Code Playgroud)
我将.path添加到目的地地址,现在可以使用了.
| 归档时间: |
|
| 查看次数: |
1534 次 |
| 最近记录: |