Can @AppStorage be used on Storyboard objects?

The need is to persist between launches the state of storyboard objects such as of type UISwitch, UITextField, etc. Can this be done using @AppStorage? If so how can @AppStorage be set to watch these?

I tried getting @AppStorage to watch an outlet class member variable that is connected to the storyboard object:

@IBOutlet weak var iPhoneName: UITextField!
@AppStorage("iPhoneName") var iPhoneName: String = ""

This got an error because the variable to be watched is already declared.

I decided to make the the watched variable different than the one connected to the Storyboard's UITextField object:

@AppStorage("iPhoneName") var striPhoneName: String = ""

and got the error: Unknown attribute 'AppStorage' . In what import library is @AppStorage defined?

If @AppStorage cannot be used for this, what is the easiest way to code storyboard object persistence? I am looking for an easy, and quick way. I am not concerned with memory usage right now.

Accepted Reply

@AppStorage is part of SwiftUI so you can't use it if you are using UIKit and storyboards for your user interface.

The UserDefaults class is the UIKit equivalent of @AppStorage.

Replies

@AppStorage is part of SwiftUI so you can't use it if you are using UIKit and storyboards for your user interface.

The UserDefaults class is the UIKit equivalent of @AppStorage.

Just import SwiftUI in the file that needs @AppStorage support, no need to hold back on this convenient wrapper just because of older code, they can co-exist.