gpt4 book ai didi

swift - 如何将 isComplate 端的 "true"值中的货币值相加?

转载 作者:行者123 更新时间:2023-12-04 08:30:34 27 4
gpt4 key购买 nike

我只是在学习 SwiftUI 方面。感谢下面的代码,我设法对所有值求和。但是,我想将货币值(value)与“isComplate = True”值相加。你能帮助我吗?

struct IncomeRow: View {

//which area where I pull the data
var todo: TODOS

var body: some View {
HStack {
Text(todo.todo)
Text(todo.category)
Text("\(todo.money)")

if todo.isComplete == "true" {
Image(systemName: "checkmark").imageScale(.medium)
} else {
Image(systemName: "xmark").imageScale(.medium)
}
}
}
}
//I sum on all values
let totalIncome = self.session.items.map({$0.money}).reduce(0, +)
let incomePrice = ("$\(totalIncome)")
//isComplate Button Action
func buttonPressed() {
if complete == true {
self.complete = false
self.session.updateTODO(key: todo.key, todo: todo.todo, money: todo.money, isComplete: "false", category: todo.category)
print("buttonpressed ran, should set complete, complete is: \(String(describing: self.complete))")
} else {
self.complete = true
self.session.updateTODO(key: todo.key, todo: todo.todo, money: todo.money, isComplete: "true", category: todo.category)
print("buttonpressed ran, should set incomplete, complete is \(String(describing: self.complete))")
}
}

最佳答案

您要使用 .filter生成仅包含已完成待办事项的数组,然后对它们求和。类似:let completedTodoIncome = self.session.items.filter { $0.isComplete == "true" }.map {$0.money}.reduce(0, +)使用字符串来表示真/假( bool )值有点奇怪。最好使用 Bool type .

关于swift - 如何将 isComplate 端的 "true"值中的货币值相加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65045387/

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