Created
January 16, 2021 05:56
-
-
Save wircho/d4de0eec86d3bf312f816c6d23b245c0 to your computer and use it in GitHub Desktop.
A possible SwiftUI bug?
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
// | |
// ContentView.swift | |
// ABCBug | |
// | |
/* | |
Is this a bug? | |
Why does pressing the button change | |
the "outer" value and not the "inner" value? | |
*/ | |
import SwiftUI | |
struct InnerContentView: View { | |
@State var number: Int | |
var body: some View { | |
Text("Inner: \(number)") | |
.padding() | |
} | |
} | |
struct ButtonView: View { | |
@Binding var number: Int | |
var body: some View { | |
Button("Change to 2"){ number = 2 } | |
} | |
} | |
struct ContentView: View { | |
@State var number = 0 | |
var body: some View { | |
VStack { | |
InnerContentView(number: number) | |
Text("Outer: \(number)") | |
.padding() | |
ButtonView(number: $number) | |
} | |
} | |
} | |
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