在 Swift 中复制 Sqlite 数据库无法正常工作 [英] Copy Sqlite DataBase in Swift does not work properly

查看:40
本文介绍了在 Swift 中复制 Sqlite 数据库无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用目标 c 的以下代码来复制 sqlite 数据库并且它工作正常.但是当我将此代码转换为 swift 时,它在 Bool 类型上显示错误.

I am using following code for objective c to copy the sqlite database and it works fine. But when I convert this code to swift it shows error on Bool type.

这是客观的c代码

  - (void) copyDatabaseIfNeeded {

  //Using NSFileManager we can perform many file system operations.
   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSError *error;

   NSString *dbPath = [self getDBPath];
   BOOL success = [fileManager fileExistsAtPath:dbPath];

  if(!success) {

   NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
   success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

   if (!success)
      NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
   }
   }

  - (NSString *) getDBPath
  {   

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
   NSString *documentsDir = [paths objectAtIndex:0];
   return [documentsDir   stringByAppendingPathComponent:@"database.sqlite"];
  }

这是导致问题的 Swift 的 CopyDataBase.

Here is the CopyDataBase for Swift which is causing issue.

   var fileManager = FileManager.default
   var error: Error!
   var dbPath = self.getDBPath()
   var success = fileManager.fileExists(atPath: dbPath)
   if !success {
   var defaultDBPath = URL(fileURLWithPath: Bundle.main.resourcePath!).appendingPathComponent("CapalinoDataBase.sqlite").absoluteString
   do {
    success = try fileManager.copyItem(atPath: defaultDBPath, toPath: dbPath)
    }
    catch {
    }
    if !success {
    assert(false, "Failed to create writable database file with message '\(error.localizedDescription)'.")
    }
      }

推荐答案

Please try this one it is working on swift 3.0

func copyDatabaseIfNeeded() {
    //Using NSFileManager we can perform many file system operations.
    let fileManager = FileManager.default
    let error: Error?
    let dbPath: String = self.getDBPath()
    var success: Bool = fileManager.fileExists(atPath: dbPath)
    if !success {
        let defaultDBPath: String = URL(fileURLWithPath: (Bundle.main.resourcePath)!).appendingPathComponent("database.sqlite").absoluteString

        do {
            success = try fileManager.copyItem(atPath: defaultDBPath, toPath: dbPath) as Any as! Bool
        }
        catch let error as NSError {
            print("Ooops! Something went wrong: \(error)")
        }

        if !success {
            assert(false, "Failed to create writable database file with message '\(error?.localizedDescription)'.")
        }
    }
}

func getDBPath() -> String {
    let paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDir: String = paths[0] as! String
    return URL(fileURLWithPath: documentsDir).appendingPathComponent("database.sqlite").absoluteString
}

这篇关于在 Swift 中复制 Sqlite 数据库无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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