Coder Social home page Coder Social logo

andrewbennet / persistedpropertywrapper Goto Github PK

View Code? Open in Web Editor NEW
40.0 3.0 2.0 39 KB

A Swift library to enable easy persistent UserDefaults storage

License: MIT License

Swift 94.34% Ruby 5.66%
swift ios macos cocoapods spm userdefaults property-wrapper swift-library

persistedpropertywrapper's Introduction

Hello world, ๐Ÿ‘‹

I'm Andrew, software developer.

I work in London at Symplectic.

On the side I'm working on an iOS app Reading List.

Twitter StackOverflow LinkedIn

persistedpropertywrapper's People

Contributors

andrewbennet avatar rob-secondstage 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

persistedpropertywrapper's Issues

Add 'projectedValue' to allow access to 'key' and 'defaultValue'

First, thanks for letting me contribute (in a small way) to your project. This is the first pull request I've ever done and the first time I've contributed anything to open source!

A suggested feature: add projectedValue to the property wrapper to allow easy access to the key and defaultValue properties. This can be useful when needing to specify these values later, for example, when using @AppStorage in SwiftUI.

Here is an example based on how I'm using this:

In the property wrapper:

    public var projectedValue: Self {
        return self
    }

Then, I use a struct to have easy access to all my persisted values:

struct Settings {
    @Persisted(key: "isFirstRun", defaultValue: true)
    static var isFirstRun

    @Persisted(key: "plantNameOrder", defaultValue: Plant.DisplayNameOrder.botanicalNameFirst)
    static var plantNameOrder: Plant.DisplayNameOrder
}

While that makes it easy to access settings throughout the app: Settings.plantNameOrder, it also, along with projectedValue makes using this in SwiftUI quite nice:

struct ContentView: View {
    @AppStorage(Settings.$plantNameOrder.key) var plantNameOrder = Settings.$plantNameOrder.defaultValue
    // ... rest of implementation 
}

Now, instead of having to make sure I get the string for key correct, and use the same default value, I can use what I've already done.

I just thought I'd suggest this based on my own usage. If you don't think this fits here, I totally get it. If you do, I'm happy to do another pull request and add something to the readme that shows this usage.

Thanks again.

Crash on model scheme change

If you change type definition of Codable instance, then you get crash immediately by force try.
Maybe Codable is not fully supported?

Redundant conformance constraint 'Convertor': 'PersistedStorageConvertor' warnings

This project is great! I've been working on something similar for my app but was having trouble with the optionals. Your approach helped me a great deal!

The current code gives Redundant conformance constraint 'Convertor': 'PersistedStorageConvertor' warnings for all of the initializer extensions. In Swift 5.3, you could use Contextual Where Clauses to fix this, and as a side benefit, you only need one extension.

Such as:

extension Persisted {
    init(_ key: String, defaultValue: Exposed, storage: UserDefaults = .standard) where Convertor == IdentityStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed {
        self.init(key: key, defaultValue: defaultValue, valueConvertor: .init(), storage: storage)
    }
    init(_ key: String, storage: UserDefaults = .standard) where Convertor == IdentityStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed? {
        self.init(key: key, defaultValue: nil, valueConvertor: .init(), storage: storage)
    }
    init(_ key: String, defaultValue: Exposed, storage: UserDefaults = .standard)  where Convertor == RawRepresentableStorageConvertor<NonOptionalExposed>, NonOptionalExposed: RawRepresentable, Exposed == NonOptionalExposed {
        self.init(key: key, defaultValue: defaultValue, valueConvertor: RawRepresentableStorageConvertor(), storage: storage)
    }
    init(_ key: String, storage: UserDefaults = .standard) where Convertor == RawRepresentableStorageConvertor<NonOptionalExposed>, NonOptionalExposed: RawRepresentable, Exposed == NonOptionalExposed? {
        self.init(key: key, defaultValue: nil, valueConvertor: RawRepresentableStorageConvertor(), storage: storage)
    }
    // Note the different parameter name in the following: encodedDataKey vs unnamed. This is reqired since some Codable types
    // are also UserDefaultsPrimitive or RawRepresentable. We need a different key to be able to avoid ambiguity.
    init(encodedDataKey key: String, defaultValue: Exposed, storage: UserDefaults = .standard) where Convertor == CodableStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed {
        self.init(key: key, defaultValue: defaultValue, valueConvertor: CodableStorageConvertor(), storage: storage)
    }
    init(encodedDataKey key: String, storage: UserDefaults = .standard) where Convertor == CodableStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed? {
        self.init(key: key, defaultValue: nil, valueConvertor: CodableStorageConvertor(), storage: storage)
    }
}

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.