简体   繁体   English

预期返回“ Int”的函数中缺少返回

[英]Missing return in a function expected to return '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'

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)
    }

}

Hi, I want to be able to return snapshot.children.allObjects.count as an integer. 嗨,我希望能够以整数形式返回snapshot.children.allObjects.count But I got this error "Missing return in a closure expected to return 'Int'" with canvasCount function. 但是我收到了带有canvasCount函数的错误“在闭包中缺少返回,期望返回'Int'”。 Anyone can help me please? 有人可以帮我吗?

You need a completion as the call to firebase is asynchronous 您需要完成,因为对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)
   }
}

Edit: ------------------------------------------------- 编辑: ------------------------------------------------ --

Declare an instance var 声明实例变量

var counter = 0

Inside viewDidLoad insert 内部viewDidLoad插入

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

Then copy those 然后复制那些

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)
    }

}

You can use completion with Int for your requirement, 您可以根据需要使用Int completion

var canvasCount: Int?

func canvasCount(completion: @escaping (_ count: Int) -> Void)  {
        ref.child("Canvas").observeSingleEvent(of: .value, with: { (snapshot) in
            completion(snapshot.children.allObjects.count)
        }) { (error) in
            print(error.localizedDescription)
            completion(nil)
        }
    }

//Get the result here
//Use weak self to avoid retain cycle
canvasCount { [weak self] (count) in
        if let count1 = count {
            self?.canvasCount = count1
            DispatchQueue.main.async {
                self?.tableView.reloadData()
            }
        }
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM