Last active
March 28, 2024 19:00
-
-
Save christianselig/4b835f6544ffe1c5c104ade74da8459e 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
import SwiftUI | |
@Observable | |
class IceCreamParlor { | |
var iceCream: IceCream? | |
} | |
@Observable | |
class IceCream { | |
var flavor: String | |
init(flavor: String) { | |
self.flavor = flavor | |
} | |
} | |
struct ContentView: View { | |
@State var parlor: IceCreamParlor | |
var body: some View { | |
VStack { | |
Text("Welcome to the Ice Cream Parlor!") | |
if let iceCream = parlor.iceCream { | |
IceCreamMakerView(flavor: $iceCream.flavor) | |
} | |
} | |
.onAppear { | |
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) { | |
parlor.iceCream = IceCream(flavor: "chocolate") | |
} | |
} | |
} | |
} | |
struct IceCreamMakerView: View { | |
@Binding var flavor: String | |
var body: some View { | |
Button { | |
flavor = ["chocolate", "vanilla", "strawberry"].randomElement()! | |
} label: { | |
Text("Random Ice Cream") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Both should work!