Coder Social home page Coder Social logo

v8tr / userdefaultspropertywrapper Goto Github PK

View Code? Open in Web Editor NEW
29.0 2.0 5.0 11 KB

Swifty UserDefaults property wrapper with statically-typed keys and change observations. https://www.vadimbulavin.com/advanced-guide-to-userdefaults-in-swift/

Home Page: https://www.vadimbulavin.com/

License: The Unlicense

Swift 100.00%
swift userdefaults user-defaults property-wrapper property-wrappers

userdefaultspropertywrapper's Introduction

Article related to this project


UserDefaultsPropertyWrapper

A sample project showing how to implement a UserDefaults property wrapper in Swift. The property wrapper has statically typed keys and provides a conveniece to observe UserDefaults changes.

Usage:

// Declare a new key
extension Key {
    static let isFirstLaunch: Key = "isFirstLaunch"
}

struct Storage {
    // Use the property wrapper to add new value to `UserDefaults`
    @UserDefault(key: .isFirstLaunch)
    var isFirstLaunch: Bool?
}

// Initialize the storage
var storage = Storage()

// Observe user default changes
var observation = storage.$isFirstLaunch.observe { old, new in
    print("Changed from: \(old) to \(new)")
}

// Make some changes
storage.isFirstLaunch = true
storage.isFirstLaunch?.toggle()

It will print:

Changed from: Optional(false) to Optional(true)
Changed from: Optional(true) to Optional(false)

userdefaultspropertywrapper's People

Contributors

v8tr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

userdefaultspropertywrapper's Issues

UserDefaults observation not trigger

Hi ! I just stumbled across your post and tried your project since I use the same technique to observe user defaults changes in our app and it's not working anymore.

So I downloaded your project, ran it and nothing was outputted. Although I think the two lines you mention should appear:

Changed from: Optional(false) to Optional(true)
Changed from: Optional(true) to Optional(false)

I wonder if it's something general for all apps with UserDefaults. To let you know, when using a key initial when registering the observer the initial value is properly received. But that's the only one.
Have you also encountered such an issue? I would love to have your thoughts on that one.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.