如何将文件从目录复制到iphone文档目录 [英] How to copy files from a directory to iphone document directory

查看:102
本文介绍了如何将文件从目录复制到iphone文档目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是,在app delegate中,我想编写一个代码,如果它不存在于iphone的文档目录中,它将复制sqlite数据库。为此,我使用以下代码 -

What I am trying to do is, in app delegate, I want to write a code that will copy a sqlite database if it is not exists in iphone's document directory. For that I am using the following code-

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let containerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()

//Create database if not exists
let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
let databaseStr = "LocalDatabase.sqlite"
let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)
let fileManager: NSFileManager = NSFileManager.defaultManager()
if !fileManager.fileExistsAtPath(dbPath) {
    let databaseInApp: String? = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent(databaseStr)
    fileManager.copyItemAtPath(databaseInApp!, toPath: dbPath, error: nil)
}

return true
}

它正在目录中创建数据库即可。但是我没有在数据库中获得ant表。这意味着创建新文件而不是复制。我确信该数据库中有9个表我要复制。

It's creating the database in the directory that is ok. But I am not getting ant tables there in database. That means new file is created instead of copying. I am sure there are 9 tables in that database which I want to copy.

文件结构如屏幕截图所示 -

Structure of files are as given in the screenshot-

我错了,我不明白。请告诉我是否有人能够解决问题。还有一件事当我在模拟器中使用
运行应用程序 /Users/Adelantelabs/Documents/Sidemenu.swift/SlideOutNavigation/Localdatabase.sqlite
然后它运行得很好,但是当我运行时它在iphone中不起作用。

Where I am wrong that I did not understand. Please tell me if anybody is able to catch the problem. One more thing When I was running the application in the simulator with the /Users/Adelantelabs/Documents/Sidemenu.swift/SlideOutNavigation/Localdatabase.sqlite Then it was working perfectly but did not work in iphone when I run it.

推荐答案

使用此代码:

let fileManager = NSFileManager.defaultManager()
var error : NSError?
var doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString
let destinationPath = doumentDirectoryPath.stringByAppendingPathComponent("LocalDatabase1.sqlite")
let sourcePath = NSBundle.mainBundle().pathForResource("LocalDatabase", ofType: "sqlite")
fileManager.copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)

这篇关于如何将文件从目录复制到iphone文档目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆