【发布时间】:2021-03-10 17:04:42
【问题描述】:
我只是在学习 SwiftUI 方面。感谢下面的代码,我设法对所有值求和。但是,我想将货币值与“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))")
}
}
【问题讨论】:
-
在 Stack Overflow 上,请不要显示文字和代码的图片。将文本复制到问题本身并设置格式,以便于阅读、复制和搜索。
-
@DougStevenson 感谢您的反馈。我修好了。
-
map(\.money)。顺便说一句Bool有一个切换方法self.complete.toggle() -
为什么这个问题用 Firebase 标记。我没有看到任何提及。