【问题标题】:How to copy files from a directory to iphone document directory如何将文件从目录复制到iphone文档目录
【发布时间】:2015-08-24 05:37:24
【问题描述】:

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

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
}

在目录中创建数据库没问题。但是我没有在数据库中获得蚂蚁表。这意味着创建新文件而不是复制。我确定该数据库中有 9 个要复制的表。

文件结构如截图所示-

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

【问题讨论】:

    标签: ios iphone swift sqlite


    【解决方案1】:

    使用此代码:

    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)
    

    【讨论】:

    • 感谢代码中的帮助。当我使用pathForResource 函数时,我使用了代码并且在 url 中得到了 nil。对于这个问题,我将我的文件拖到Copy Bundle Resources 下,它工作了..
    【解决方案2】:
       func copyDatabase(){
    
                let fileManager = NSFileManager.defaultManager()
    
            let dbPath = getDBPath()
            var success = fileManager.fileExistsAtPath(dbPath)
    
    
            if(!success) {
              if let defaultDBPath = NSBundle.mainBundle().pathForResource("LocalDatabase", ofType: "sqlite"){
    
              var error:NSError?
              success = fileManager.copyItemAtPath(defaultDBPath, toPath: dbPath, error: &error)
              println(defaultDBPath)
              if (!success){
              println("Failed to create writable database file with message\(error!.localizedDescription))")
              }
              }else{
                println("Cannot Find File In NSBundle")
              }
            }else{
              println("File Already Exist At:\(dbPath)")
            }
          }
    
    
          func getDBPath()->String
          {
    
          let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
          let documentsDir = paths[0] as! String
          let databasePath = documentsDir.stringByAppendingPathComponent("LocalDatabase.sqlite")
          return databasePath;
          }
    

    然后在didFinishLaunching调用它:

      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
         copyDatabase()
        return true
    }
    

    【讨论】:

    • 感谢代码中的帮助。当我使用pathForResource 函数时,我使用了代码并且在 url 中得到了 nil。对于这个问题,我将我的文件拖到Copy Bundle Resources 下,它工作了..
    【解决方案3】:

    Swift 3.0 版本

    let fileManager = FileManager.default
    var doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
    let destinationPath = doumentDirectoryPath.appendingPathComponent("LocalDatabase.sqlite")
    let sourcePath = Bundle.main.path(forResource: "LocalDatabase", ofType: "sqlite")
    do{
      try fileManager.copyItem(atPath: sourcePath!, toPath: destinationPath)
    }catch let error as NSError {
      print("error occurred, here are the details:\n \(error)")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      相关资源
      最近更新 更多