When I try to replicate the example from SwiftUI documentation - https://developer.apple.com/documentation/swiftui/toggle/onchange%28of%3Aperform%3A%29 on onChange modifier and capture the previous value, inside the closure the previous value is equal to the new value, not the old one.
struct ContentView: View {
@State private var isSubscribed = false
var body: some View {
Toggle("Subscribe", isOn: $isSubscribed)
.onChange(of: isSubscribed) { [isSubscribed] newState in
print(isSubscribed)
print(newState)
}
}
}
In the documentation - https://developer.apple.com/documentation/swiftui/toggle/onchange%28of%3Aperform%3A%29 example, the onChange modifier is attached to the body property, not to a view inside it. Is this how it should be used? Attaching it to the body property in Xcode 12.0 beta (12A6159) doesn't compile.