Swift将数据从函数传递到另一个函数

时间:2017-11-27 15:50:47

标签: swift firebase

我有一个小的快速应用程序,其中包括3个函数。

getIncData()getExpData()(从firebase获取数据并将其存储在数组中)和getLeftBudget(),它应该从getIncData()计算数组的总值 - { {1}}

以下是我的代码的样子

getExpData()

现在var income = [Double]() var expenses = [Double]() var totalIncome: Double = 0 var totalExpenses: Double = 0 func getIncData() { let budgetDB = Database.database().reference().child("Income") budgetDB.observe(.childAdded) { (snapshot) in let snapshotValue = snapshot.value as! Dictionary<String, String> let value = snapshotValue["Value"]! self.income.append(Double(Int(value)!)) self.totalIncome = self.income.reduce(0, +) self.incomeLabel.text = "Income: " + String(self.totalIncome) + " €"; } } func getExpData() { let budgetDB = Database.database().reference().child("Expenses") budgetDB.observe(.childAdded) { (snapshot) in let snapshotValue = snapshot.value as! Dictionary<String, String> let value = snapshotValue["Value"]! self.expenses.append(Double(value)!) self.totalExpenses = self.expenses.reduce(0, +) self.expensesLabel.text = "Expenses: " + String(Double(self.totalExpenses)) + " €"; } func getLeftBudget() { let budgetLeft = self.totalIncome - self.totalExpenses self.budgetMonth.text = "Budget left - \(String(budgetLeft))" } 内的self.totalIncomeself.totalExpenses返回0.什么是将函数数据转换为第三个函数以便进行计算的最佳方法?

P.S。数组将填充来自firebase的数据,计算在getLeftBudgetgetIncData()

内完成

0 个答案:

没有答案