Skip to content

An `AppStorage` like property wrapper for saving/restoring the `Dictionary` type value.

License

Notifications You must be signed in to change notification settings

sussan0416/DictionaryDefaults

Repository files navigation

DictionaryDefaults

This is an AppStorage like property wrapper for saving/restoring the Dictionary type value.

Usage

import DictionaryDefaults

struct ContentView: View {
    @DictionaryDefaults(key: "my_dict")
    private var myDictionary = [String: Any]()

    var body: some View {
        List(keys, id: \.self) { key in
            Text(myDictionary[key] as! String)
        }

        Button(
            action: {
                // As soon as the dictionary is changed, the change is saved in UserDefaults
                myDictionary.updateValue("FirstView", forKey: UUID().uuidString)
            },
            label: {
                Text("Add")
            }
        )

        // Binding is OK
        OtherView(dict: $myDictionary)
    }
}

About

An `AppStorage` like property wrapper for saving/restoring the `Dictionary` type value.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages