预期返回"Int"的函数中缺少返回 [英] Missing return in a function expected to return 'Int'
本文介绍了预期返回"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'
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'".有人可以帮我吗?
Hi, I want to be able to return snapshot.children.allObjects.count
as an integer. But I got this error "Missing return in a closure expected to return 'Int'" with canvasCount
function. Anyone can help me please?
推荐答案
您需要完成,因为对firebase的调用是异步的
You need a completion as the call to firebase is asynchronous
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)
}
}
这篇关于预期返回"Int"的函数中缺少返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文