[英]SwiftUI and Combine - Update variable in view model when another changes
I am currently building a small one view app to help learn some more SwiftUI.我目前正在构建一个小的单一视图应用程序来帮助学习更多的 SwiftUI。 The app is a simple app to calculate values in a ratio.该应用程序是一个简单的应用程序,用于计算比率值。 I have 4 main variables at the moment, the left and right side of the ratio, and the first and second value that will be calculated in ratio.我目前有 4 个主要变量,比率的左侧和右侧,以及将以比率计算的第一个和第二个值。 Initially I had each of these stored as @State
properties:最初,我将其中的每一个都存储为@State
属性:
@State var leftSide = Double()
@State var rightSide = Double()
@State var value1 = Double()
@State var value2 = Double()
In the app there are 4 sliders that allow each variable to be changed independently of each other.在应用程序中有 4 个滑块,允许每个变量相互独立地更改。 What I want to now allow to happen is that changing the variable value1
will also update value2
according to the ratio the user has set.我现在希望允许发生的是,更改变量value1
也将根据用户设置的比率更新value2
。 To achieve this I believe I need to use Combine however I have not got much experience with the framework.为了实现这一点,我相信我需要使用Combine,但是我对该框架没有太多经验。
To begin with I moved the variables to an ObservableObject
class:首先,我将变量移动到ObservableObject
类:
final class UserData: ObservableObject {
@Published var leftSide = Double()
@Published var rightSide = Double()
@Published var value1 = Double()
@Published var value2 = Double()
}
I updated my main view with @ObservedObject var userData = UserData()
so I could access the variables in my view and this works fine我用@ObservedObject var userData = UserData()
更新了我的主视图,这样我就可以访问视图中的变量,这很好用
Now I want to update the value1 and value2 variables when the other changes.现在我想在其他更改时更新 value1 和 value2 变量。 From the reading I have done I think I need to use a subscriber but I cannot find anything on how to set this up with they @Published
property.根据我所做的阅读,我认为我需要使用订阅者,但我找不到关于如何使用他们的@Published
属性进行设置的任何信息。 Have I gone about this in the correct way or is there a better way to do this?我是否以正确的方式解决了这个问题,或者有更好的方法来做到这一点?
I have 4 main variables at the moment, the left and right side of the ratio, and the first and second value that will be calculated in ratio我目前有 4 个主要变量,比率的左侧和右侧,以及将在比率中计算的第一个和第二个值
so, why you don't declare it as computed variables?那么,为什么不将其声明为计算变量?
final class UserData: ObservableObject {
@Published var leftSide = 0.0
@Published var rightSide = 0.0
var value: Double {
// perform the calculation here
...
}
}
If you like to change value independently (bind it to SwiftUI component), and modify the value in case leftSide or rightSide are modified, use willSet or even didSet如果你喜欢独立改变值(绑定到 SwiftUI 组件),并且在修改 leftSide 或 rightSide 的情况下修改值,使用 willSet 甚至 didSet
final class UserData: ObservableObject {
@Published var leftSide = 0.0 {
willSet {
// update value
value = f(newValue)
}
didSet {
// update value
value = f(oldValue)
}
}
@Published var rightSide = 0.0
@Published var value = 0.0
func f(_ v: Double)->Double {
......
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.