ios - 预期返回“Int”的函数中缺少返回
问题描述
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'
预期返回“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
整数形式返回。但是我收到了这个错误“在预期返回'Int'的闭包中缺少返回”和canvasCount
函数。任何人都可以帮助我吗?
解决方案
您需要完成,因为对 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)
}
}
推荐阅读
- proxy - Nuxt js:如何在 laravel sanctum 中使用代理?
- javascript - 如何使用 Victory Charts 特别是 Victory Candlestick 错开 x 轴值
- java - 基数排序的并行版本未按预期运行(Java)
- android - onCreateOptionsMenu 永远不会被调用(工具栏膨胀)
- unity3d - 我如何获取和渲染 sprite 表单 dataPath?
- java - 有没有办法将Extra放入对话框
- r - 在 R 中过滤日期
- java - 页面未调整大小/向其他用户显示不同的问题
- python - 导入 Sklearn 时出现错误
- r - 为什么在 mapply 的 MoreArgs 参数中进行变量赋值与在其“...”参数中进行变量赋值给出不同的结果?