Skip to content

Instantly share code, notes, and snippets.

@wircho
Created January 16, 2021 05:56
Show Gist options
  • Save wircho/d4de0eec86d3bf312f816c6d23b245c0 to your computer and use it in GitHub Desktop.
Save wircho/d4de0eec86d3bf312f816c6d23b245c0 to your computer and use it in GitHub Desktop.
A possible SwiftUI bug?
//
// 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