Skip to content

Instantly share code, notes, and snippets.

@SatoTakeshiX
Created November 23, 2020 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SatoTakeshiX/15446b7838625c9c2959c2e50d692703 to your computer and use it in GitHub Desktop.
Save SatoTakeshiX/15446b7838625c9c2959c2e50d692703 to your computer and use it in GitHub Desktop.
final class DataSource: ObservableObject {
@Published var counter = 0
}
struct StateObjectCounterView: View {
@StateObject private var dataSource = DataSource()
var body: some View {
VStack {
Button("increment counter") {
dataSource.counter += 1
}
Text("StateObject count: \(dataSource.counter)")
.font(.title)
}
}
}
struct ObservedObjcetCounterView: View {
@ObservedObject private var dataSource = DataSource()
var body: some View {
VStack {
Button("increment counter") {
dataSource.counter += 1
}
Text("ObservedObject count: \(dataSource.counter)")
.font(.title)
}
}
}
struct SwitchColorView: View {
@State private var isDanger: Bool = false
var body: some View {
VStack {
Button("Change the Color") {
isDanger.toggle()
}
if isDanger {
Circle().foregroundColor(.red)
.frame(width: 200, height: 200)
} else {
Circle().foregroundColor(.green)
.frame(width: 200, height: 200)
}
StateObjectCounterView()
ObservedObjcetCounterView()
Spacer()
}
}
}
struct CountView_Previews: PreviewProvider {
static var previews: some View {
SwitchColorView()
.previewLayout(.fixed(width: 200, height: 400))
}
}
@SatoTakeshiX
Copy link
Author

Nov-23-2020 18-14-11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment