gpt4 book ai didi

ios - 预期返回 'Int' 的函数中缺少返回值

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:48 25 4
gpt4 key购买 nike

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
canvasCount { (value) in
if let res = value {
return res
}
} //Missing return in a closure expected to return 'Int'
} //Missing return in a closure expected to return 'Int'

Missing return in a closure expected to return 'Int'

func canvasCount(completion:@escaping((_ va:Int?) -> Int )) {

ref.child("Canvas").observeSingleEvent(of: .value, with: { (snapshot) in
completion( snapshot.children.allObjects.count)
}) { (error) in
print(error.localizedDescription)
completion(nil)
}

}

您好,我希望能够以整数形式返回 snapshot.children.allObjects.count。但是我用 canvasCount 函数得到了这个错误“在一个期望返回‘Int’的闭包中缺少返回”。任何人都可以帮助我吗?

最佳答案

您需要完成,因为对 firebase 的调用是异步的

func canvasCount(completion:@escaping((_ va:Int?) -> () )) { 

ref.child("Canvas").observeSingleEvent(of: .value, with: { (snapshot) in
completion( snapshot.children.allObjects.count)
}) { (error) in
print(error.localizedDescription)
completion(nil)
}

}

canvasCount { (value) in 
if let res = value {
print(res)
}
}

编辑 ------------------------------------ ----------

声明一个实例变量

var counter = 0

viewDidLoad 内插入

canvasCount { (value) in
if let res = value {
self.counter = res
self.tableView.reloadData()
}
}

然后复制那些

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return counter
}


func canvasCount(completion:@escaping((_ va:Int?) -> ())) {

ref.child("Canvas").observeSingleEvent(of: .value, with: { (snapshot) in
completion( snapshot.children.allObjects.count)
}) { (error) in
print(error.localizedDescription)
completion(nil)
}

}

关于ios - 预期返回 'Int' 的函数中缺少返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53758761/

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