Post

Replies

Boosts

Views

Activity

Reply to How to avoid Swift 6 concurrency warning from UIAccessibility.post()
Thanks for the quick reply and good to know it's not me! (it usually is 😆) I'm importing SwiftData and SwiftUI, so I tried @preconcurrency import SwiftUI and it does suppress the warning. But I'm new at this and I'm not sure if that's OK or if I should be importing UIKit instead? Code seems to work so I'm guessing there's a lot of overlap, but I'd like to avoid any unintended side effects down the road.
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’24
Reply to Observation and MainActor
Now that @Observable macro has been out a little bit, what's the latest thinking on this? 🤔 How would one apply @MainActor to the ViewModel of the following code? Currently, doing so gives the "Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context" error on the @State line in the TestApp struct. import SwiftUI @main struct TestApp: App { @State private var vm = ViewModel() var body: some Scene { WindowGroup { ContentView() .environment(vm) } } } @Observable @MainActor class ViewModel { var showDetails: Bool = false } struct ContentView: View { @Environment(ViewModel.self) var vm var body: some View { @Bindable var vm = vm VStack { DetailsButton(showDetails: $vm.showDetails) if vm.showDetails { Text("This is my message!") } } } } struct DetailsButton: View { @Binding var showDetails: Bool var body: some View { Button("\(showDetails ? "Hide" : "Show") Details") { showDetails.toggle() } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’23