I'm reading the Apple document, but I don't understand it.
Documents
class Weather {
@Published variable temperature—Double
init(temperature:Double) {
self.temperature=temperature
}
}
let weather = Weather (temperature:20)
cancelable=weather.$temperature
.sink(){
print("Temperature now:\($0)")
}
weather.temperature=25
If you do this,
// Temperature now: 20.0
// Temperature now:25.0
It says, but why is 20.0?
After generating the Weather class, that is, the temperature has already entered and the publisher is registered afterwards, so
I don't think the publisher will work for 20, but it will actually output 20.
Please let me know if you understand.
swift swiftui combine
I guess weather.temperature is the CurrentValueSubject.
Subject|Apple Developer Documentation
The expected behavior seems to be from another subject, PassthroughSubject.
e.g.
let relay=PassthroughSubject<String, Never>()
relay.send("Hello") // no output
let subscription=relay
.sink {value in
print("subscription1 received value:\(value)")
}
relay.send("World!")//=>World!
Comparison Code
let variable=CurrentValueSubject<String, Never>("")
variable.send("Initial text")
let subscription2 = variable.sink {value in
print("subscription2 received value:\(value)")
}
variable.send("More text")
// subscription2 received value —Initial text
// subscription2 received value:More text
568 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
891 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
610 Uncaught (inpromise) Error on Electron: An object could not be cloned
600 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.