【发布时间】:2018-10-16 22:59:45
【问题描述】:
这个问题已经被问过很多次了,但在我的特殊情况下似乎没有任何效果。正如您在下面的代码中看到的那样,我为 UITableViewDataSource 创建了 View Controller 的扩展。我正在尝试从我的 Firebase 实时数据库中提取信息,并根据我得到的信息返回某个 Int。问题是,每当我尝试从 Firebase 快照中返回一个 Int 时,我都会收到此错误:“在 void 函数中出现意外的非 void 返回值。”我理解这样做的原因,但我不确定如何使我的代码正常工作。有什么解决办法吗?
我的代码:
extension LikeOrDislikeViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("Num Liked").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let numMoviesLiked = ((dictionary["Number of Movies Liked"] as? Int))!
if numMoviesLiked%4 == 0 {
return numMoviesLiked/4
}
}
})
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = UIColor.red
tableView.rowHeight = 113
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
【问题讨论】:
标签: ios swift firebase firebase-realtime-database