Swift NumberOfRowsInSection在void函数中出现意外的非void返回值

时间:2018-11-01 15:19:13

标签: swift firebase firebase-realtime-database tableview

我要显示或不显示我的“ NoPostsView”,具体取决于我的Firebase数据库。

这是我用来查找是否有帖子的功能:

func checkIfPostsExists(YES: @escaping () -> Void, NO: @escaping () -> Void)
{
     REF_POSTS.observeSingleEvent(of: .value, with:
     {
         (snapshot) in
         if snapshot.exists()
         {
             YES()
         }
         else
         {
             NO()
         }
     })
}

这就是我的使用方式:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    checkIfPostsExists(YES:
    {
        if self.posts.count <= 0
        {
            return 7 // ERROR IS IN THIS LINE
        }
        else if self.posts.count != 0
        {
            self.noPostsView.isHidden = true
        }
    })
    {
        self.noPostsView.isHidden = false
    }
    return posts.count
}

我得到的错误是:

  

void函数中意外的非void返回值

我很确定这是由于我在该函数中返回7而引起的,但是我似乎无法解决这个错误。

1 个答案:

答案 0 :(得分:0)

您应该能够执行以下操作……

// uses a completion with a boolean value to show whether has posts or not
private func hasPosts(completion: (Bool) -> Void) {
    // check posts and then at some point return a boolean
    completion(true) 
}

// check if has posts and either show tableView or noPostsView
override func viewDidLoad() {
   super.viewDidLoad() 

   hasPosts() { hasPosts in 
      if hasPosts {
          tableView.dataSource = self
          // maybe other logic
      }
      noResultsView.isHidden = hasPosts
      tableView.isHidden = !hasPosts
   }
}