gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:11:47 27 4
gpt4 key购买 nike

我有一个小型的 swift 应用程序,其中包含 3 个功能。

getIncData()getExpData()(从 firebase 获取数据并将其存储在数组中)和 getLeftBudget() 应该从 getIncData() - 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))"
}

现在 getLeftBudget 中的 self.totalIncomeself.totalExpenses 返回 0。从函数中获取数据的最佳方法是什么进入第三个函数以便我可以进行计算?

附言数组中填充了来自 firebase 的数据,计算在 getIncData()getExpData()

中完成

最佳答案

您正在使用 budgetDB.observe 从异步函数获取值。

然后当您调用 getLeftBudget 时,您还没有变量中的值。您应该实现一个完成处理函数。

此代码未经测试,我只是在外出时用记事本适配,但思路就是这样。

var income = [Double]()
var expenses = [Double]()
var totalIncome: Double = 0
var totalExpenses: Double = 0

typealias returnCalc = (Bool) -> Void

func getIncData(completion: @escaping returnCalc) {
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) + " €";
completion(true)
}
}

func getExpData(completion: @escaping returnCalc) {

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)) + " €";
completion(true)
}
}

func getLeftBudget() {
getIncData { (completed) in
getExpData(completion: { (completed) in
let budgetLeft = self.totalIncome - self.totalExpenses
self.budgetMonth.text = "Budget left - \(String(budgetLeft))"
})
}
}

你可以稍微插入完成 block 的使用,直接使用函数返回值,避免上下文或全局变量:

var income = [Double]()
var expenses = [Double]()
var totalIncome: Double = 0
var totalExpenses: Double = 0

typealias returnCalc = (Double) -> Void

func getIncData(completion: @escaping returnCalc) {
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.income.reduce(0, +)) + " €";
completion(self.income.reduce(0, +))
}
}

func getExpData(completion: @escaping returnCalc) {

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.expenses.reduce(0, +))) + " €";
completion(self.expenses.reduce(0, +))
}
}

func getLeftBudget() {
getIncData { (inc) in
getExpData(completion: { (exp) in
let budgetLeft = inc - exp
self.budgetMonth.text = "Budget left - \(String(budgetLeft))"
})
}
}

关于Swift 将数据从一个函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47514867/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com