gpt4 book ai didi

ios - 错误 : Invalid conversion from throwing function of type '(_) throws -> ()' to non-throwing function type '(DataSnapshot) -> Void'

转载 作者:可可西里 更新时间:2023-11-01 00:57:05 38 4
gpt4 key购买 nike

这是我的代码:

func loadData() {
ref.child(currentUserID!).observe(.childAdded) {
(snapshot) in
let snapshotValue = try? snapshot.value as? [String: AnyObject]
if let item = try TableViewModel(id: snapshot.key, likeLabel: self.likeLabel, playLabelString: self.playLabelString, json: snapshotValue) {
self.items.append(item)
}
self.tableViewModel = self.items.reversed() as [TableViewModel]
}
}

我无法理解以下错误:

Invalid conversion from throwing function of type (_) throws -> () to non-throwing function type(DataSnapshot) -> Void

在下面一行中:

ref.child(currentUserID!).observe(.childAdded) {

请帮忙。

最佳答案

您的主要问题是以下表达式:

if let item = try TableViewModel(...) {

try 命令指示您的 Firebase 完成闭包可能 抛出错误,这是您传递闭包的 observe API 未预料到的情况到,因此您遇到的编译错误。

好的,除此之外,我相信您已经想到了 try? 运算符。如果是这样,试试这个:

if let item = try? TableViewModel(...) {

或者简单地说:

if let item = TableViewModel(...) {

如果 TableViewModel 根本没有抛出任何错误(即它只是一个失败的初始化程序)。

顺便说一句,您的 snapshotValue 变量也可以使用一些帮助:

let snapshotValue = snapshot.value as! [String: AnyObject]

关于ios - 错误 : Invalid conversion from throwing function of type '(_) throws -> ()' to non-throwing function type '(DataSnapshot) -> Void' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44599200/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com