Man*_*olo 2 sqlite database-connection ios swift
我从文档中得到了这个代码,我找不到文件的路径,我需要将"contacts.db"文件从Supporting Files文件夹复制到设备中不在模拟器中的应用程序以供离线使用.
func copyItemAtPath(_ srcPath: String,
toPath dstPath: String,
error error: NSErrorPointer) -> Bool
Run Code Online (Sandbox Code Playgroud)
srcPath =要移动的文件或目录的路径.此参数不得为零.
dstPath =放置srcPath副本的路径.此路径必须包含新位置中的文件或目录的名称.此参数不得为零.
error = On输入,指向错误对象的指针.如果发生错误,则将此指针设置为包含错误信息的实际错误对象.如果您不想要错误信息,可以为此参数指定nil.
任何帮助都非常感谢.:)
您只需将"contacts.db"拖放到Project Navigator中,然后就可以通过这种方式找到该文件的路径:
let sourcePath = NSBundle.mainBundle().pathForResource("contacts", ofType: "db")
Run Code Online (Sandbox Code Playgroud)
之后,您可以将该文件复制到应用程序的文档文件夹中,为此您需要该文档文件夹路径:
let doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
let destinationPath = doumentDirectoryPath.stringByAppendingPathComponent("contacts1.db")
Run Code Online (Sandbox Code Playgroud)
现在你有sourcePath,destinationPath所以你可以将该文件复制到文件夹中:
NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
Run Code Online (Sandbox Code Playgroud)
如果你想检查错误,你可以这样做:
var error : NSError?
NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
if let error = error {
println(error.description)
}
Run Code Online (Sandbox Code Playgroud)
如果你得到你的sourcePathnil然后转到Targets-> YouApp-> Build Phases-> copy Bundle Resources并在那里添加你的文件.希望这会有所帮助.
| 归档时间: |
|
| 查看次数: |
5679 次 |
| 最近记录: |