【问题标题】:FMDatabaseQueue is not surviving a class init()FMDatabaseQueue 没有在类 init() 中幸存
【发布时间】:2015-02-13 22:07:53
【问题描述】:

我一直在尝试将代码初始化程序放在模型类的init() 函数中,我有这样的:

 init?() {
    let docsDir =     NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    let dbPath = docsDir.stringByAppendingPathComponent("app.sqlite")
    let dbQueue = FMDatabaseQueue(path: dbPath)

    if dbQueue == nil {
        NSLog("%@ : %@", "Problem connecting to the db at", __FUNCTION__)
        return nil
    } else {
        NSLog("%@", "Successfully connected to the db")
    }
}

当我尝试从该类的另一个函数中使用 dbQueue 时,dbQueue 总是等于 nil 之类的

 func anotherFunction() {
   dbQueue?.inDatabase{ db in .... }
 }

所以当我尝试初始化时:

 let db = dbConnector()
 db.anotherFunc() // Zilch!

我的问题是为什么会发生这种情况以及如何解决它。请帮忙,我会请托尔祝福你

【问题讨论】:

    标签: ios swift fmdb


    【解决方案1】:

    我找到了解决方案。我在类属性dbQueue 上使用let dbQueue,它实际上是在init() 内创建一个同名的局部变量,使属性未初始化,因此nil

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2023-03-11
      • 2016-11-24
      • 2022-11-13
      • 2016-06-19
      • 2017-06-12
      • 2013-01-19
      相关资源
      最近更新 更多