Swift Firebasefunction返回
我在返回函数中遇到Firebase值很困难。 对我来说,这似乎是一个持续存在的问题。 我已经写了一个关于我的问题的基本例子。 我该怎么做呢?
func getChartIndexValues(completion:@escaping (Double) -> ()) { //Firebase Initialization var ref: FIRDatabaseReference! ref = FIRDatabase.database().reference() ref.child("general_room_index").observeSingleEvent(of: .value, with: {(snapshot) in let snapDict = snapshot.value as? NSDictionary var zero = snapDict?["0"] as! Double completion(zero) }) } returnFunction() -> (Double) { getChartIndexValues() { (zero) -> () in let testValue = zero } return //THis is my problem }
你已经暗示了你的问题,但没有明确说明。 这笔交易是你不能将async函数的结果作为函数结果返回。 您需要传入一个在函数完成时运行的完成处理程序,并且该代码是可以访问结果的代码。
returnFunction() -> (Double) { getChartIndexValues() { (zero) -> () in //Your code to process the results belongs here self.someInstanceVar = zero someLabel.text = "Your result is \(zero)" } } //You CAN'T put your code to process results here.
- 如何在单元格中设置当前用户? Swift,parsing
- iOS – PDF在UIWebView中加载,然后使用UIDocumentInteractionController保存到iBooks
- 如何使用Titanium创建凸起的标签栏项目,如Instagram中的标签栏项目?
- UIBezierPath的起始和结束角度?
- 从拍摄的真实物体360度旋转物体
- AFNetworking嵌套json中的post image
- Interface Builder:使用“Aspect Fit”时自动为UIImageView设置宽高比?
- 在Swift 2中对字典数组进行sorting
- iOS静音推送通知仅在连接到xcode时有效