【问题标题】:Firebase Syntax with Swift 3.0Swift 3.0 的 Firebase 语法
【发布时间】:2016-12-09 04:18:39
【问题描述】:

以下是一些从我的 iOS 应用中获取的与 Firebase 相关的代码:

var dataRef = firebaseRootReference?.child(byAppendingPath: "MyList")
dataRef = dataRef?.child(byAppendingPath: key)

dataRef.observe(.value, with: { snapshot in
    .......
    }, withCancel: { error in
        print(error.description)
})

上面的代码运行良好。但我刚刚将我的项目升级到 Swift 3.0,现在我收到了以下错误消息:

无法将类型 '(_) -> ()' 的值转换为预期的参数类型 '((Error?) -> Void)!'

浏览网络我只能找到类似代码的示例,没有错误处理部分。 如果我只是像下面这样注释掉一个部分,我会摆脱错误,但我怀疑这是正确的解决方案。

var dataRef = firebaseRootReference?.child(byAppendingPath: "MyList")
dataRef = dataRef?.child(byAppendingPath: key)

dataRef?.observe(.value, with: { snapshot in
    .......
    }/*, withCancel: { error in
        print(error.description)
}*/)

在 Swift 3.0 中编写上述代码的正确方法是什么?

【问题讨论】:

  • child(byAppendingPath: "MyList") 已在 Swift 3 中弃用,替换为 child("MyList")

标签: ios swift firebase swift3 firebase-realtime-database


【解决方案1】:

由于print(error.description) 行,您收到此错误。 Error 协议没有description 属性,只需将其更改为localizedDescription 即可解决您的问题。

print(error.localizedDescription)

整个代码如下所示。

dataRef?.observe(.value, with: { snapshot in

    //get data from snapshot object
}, withCancel: { error in

    print(error.localizedDescription)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    相关资源
    最近更新 更多