Post

Replies

Boosts

Views

Activity

Setting UserDefaults in Preview
Hello, I've got a View that loads data from UserDefaults. I want to set the value of the UserDefault in the preview so i can see how it looks while developing. However when i am trying to set it in preview, i get the following error when i try set it in preview. 'buildExpression' is unavailable: this expression does not conform to 'View' What is the correct way to set the user defaults in preview? import Foundation class PreferencesViewModel: ObservableObject { @Published var maximumDistance: Double = UserDefaults.standard.value(for: .maximumDistance) as? Double ?? PreferencesViewModel.maximumDistanceOptions[0] { didSet { UserDefaults.standard.set(maximumDistance, for: .maximumDistance) } } #Preview { let preferencesViewModel = PreferencesViewModel() preferencesViewModel.maximumDistance = 5.0 PreferencesView() .environmentObject(PreferencesViewModel()) }
2
0
78
Apr ’25