-
-
Save stephencelis/3a232a1b718bab0ae1127ebd5fcf6f97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The `Binding.init?(_:)` is unsafe to use and can crash when state goes from non-`nil` to `nil`. | |
// | |
// To reproduce: | |
// | |
// * Paste this code over a fresh SwiftUI project's "ContentView.swift" | |
// * Run the preview or application | |
// * Tap "Bool?" to show the toggle | |
// * Tap "Bool?" again to try to hide it and the application will crash. | |
import SwiftUI | |
struct ContentView: View { | |
@State var bool: Bool? | |
var body: some View { | |
VStack { | |
Button("Bool?") { | |
self.bool = self.bool == nil ? false : nil | |
} | |
if let $bool = Binding(self.$bool) { | |
Toggle("Bool", isOn: $bool) | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rayfix The episode code is simplified, but I believe swiftui-navigation handles animation correctly. It does so by writing through the binding's transaction:
https://github.com/pointfreeco/swiftui-navigation/blob/7a61fb419997f56bd9928c7b405fc7557a60d4bd/Sources/SwiftUINavigation/Binding.swift#L41
(Thanks to @iampatbrown for that particular solution to the problem!)