【发布时间】:2021-07-14 17:14:49
【问题描述】:
我现在正在学习 swiftui,我是 stackoverflow 的新手,我发现一个问题,这是我的代码。我想更改 sink 中的@State nopubName,但它不起作用,打印总是“Nimar”,我不知道为什么
struct ContentView: View {
@State var nopubName: String = "Nimar"
private var cancellable: AnyCancellable?
var stringSubject = PassthroughSubject<String, Never>()
init() {
cancellable = stringSubject.sink(receiveValue: handleValue(_:))
}
func handleValue(_ value: String) {
print("handleValue: '\(value)'")
self.nopubName = value
print("in sink "+nopubName)
}
var body: some View {
VStack {
Text(self.nopubName)
.font(.title).bold()
.foregroundColor(.red)
Spacer()
Button("sink"){
stringSubject.send("World")
print(nopubName)
}
}
}
}
【问题讨论】: