UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

@AppStorage

Forums > SwiftUI

Hi, is it possible to store a set of boolean variables by using @AppStorage.

Currently I use e.g. this situation:

class UserSettings: ObservableObject {

 @Published var localTime : Bool {
        didSet {
             UserDefaults.standard.set(localTime, forKey:"local")
         }
     }
      ...   

     init() {
      self.localTime = UserDefaults.standard.object(forKey: "local") as? Bool ?? false

      ...
     }
}

Thanks for your support.

Peter

3      

You can do this

@AppStorage("local") var localTime: Bool = false

then to change

localTime = true

3      

hi,

for a single value or a few values is it clear. But what if I have a series of such values? That's my intention. To say it clearly: I have a bunch of single Booleans and I would like to pass them together via @AppStorage to UserDefaults.

Sorry if I was not clear enough.

Peter

4      

I was looking at the same problem today. I have a bunch of them and wanted to pass them together as a struct, but apparently it is not supported. We can use the following types: Bool, Int, Double, String, URL and Data. Store a codable element unfortunately so far this doesn’t seem to be possible. There is this article about this and some hacks but it seems to be quite a hassle! https://fivestars.blog/swiftui/app-scene-storage.html

3      

ok. I see that is the current state of things.

Nevertheless, thanks for information.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.