TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

@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      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.