Skip to content

Instantly share code, notes, and snippets.

@christianselig
Created February 6, 2024 21:24
Show Gist options
  • Save christianselig/792257d1270f0c6dd3ebd9fde062ed51 to your computer and use it in GitHub Desktop.
Save christianselig/792257d1270f0c6dd3ebd9fde062ed51 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
@State var viewModel = CoolViewModel()
var body: some View {
VStack {
Menu {
Picker("Ice Cream", selection: $viewModel.selectedIceCream) {
if let iceCreams = viewModel.iceCreams {
ForEach(iceCreams, id: \.self) { iceCream in
Text(iceCream)
.tag(iceCream)
}
}
}
} label: {
Text("Choose")
}
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
viewModel.selectedIceCream = "vanilla"
viewModel.iceCreams = ["chocolate", "vanilla", "strawberry"]
}
}
}
}
@Observable
class CoolViewModel {
// These vars come in async, so aren't available right away
var selectedIceCream: String?
var iceCreams: [String]?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment