1

I‘d like to store my user defaults in a class. With the old Swift data flow it was possible to use it in a class. Now, with the new system (Xcode 15, Beta 2), it doesn’t work anymore. Is there any way to use it? Maybe a workaround?

This is how it worked before:

import SwiftUI

class UserInfo: ObservableObject {

@AppStorage("username") var username: String = ""

}

And now I want to use the new system:

import SwiftUI

@Observable class UserInfo {

@AppStorage("username") var username: String = ""

}

Unfortunately, it doesn’t work.

1 Answers1

1

Using @AppStorage ... in class UserInfo: ObservableObject works well at updating the UserDefaults. It is not observed as when you use @Published var ... variables.

Similarly to use the equivalent in the new @Observable, try using:

 @Observable class UserInfo { 

       @ObservationIgnored @AppStorage("username") var username: String = ""

 }