Coder Social home page Coder Social logo

davidsteppenbeck / observableuserdefault Goto Github PK

View Code? Open in Web Editor NEW
64.0 64.0 7.0 35 KB

A Swift macro that adds accessor blocks to properties in `Observable` classes that read and write values in `UserDefaults`.

License: MIT License

Swift 100.00%
macro observable swift swiftui userdefaults

observableuserdefault's People

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  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

observableuserdefault's Issues

Error: '@ObservableUserDefault' unable to extract the required values from the argument

I'm trying to use this library based on the examples provided, but I get the following error:

// Error: '@ObservableUserDefault' unable to extract the required values from the argument
@ObservableUserDefault(.init(key: "systemPrompt", defaultValue: "You are a helpful assistant.", store: UserDefaults(suiteName: "\(appGroupIdentifier).settings")!))
@ObservationIgnored
var systemPrompt: String

Support optional types when providing an explicit key.

I noticed that optional types work when using ObservableUserDefault without metadata, but fail to extract type information when a key is provided along with an optional type. This is important because providing keys is necessary for backwards compatibility, while most of our user defaults are optional. Relatedly, it would be nice if the api allowed optional defaultValue and store arguments to match the current AppStorage apis.

Successful

@ObservableUserDefault
@ObservationIgnored
public var lastViewed: Date?

Error

@ObservableUserDefault(.init(key: "LastViewedKey", defaultValue: nil, store: .standard))
@ObservationIgnored
public var lastViewed: Date?

The error is of the message Generic parameter 'DefaultValue' could not be inferred.

Attempting to provide type hinting gives a different error:

Error with hinting

@ObservableUserDefault(.init(key: "LastViewedKey", defaultValue: nil as Date?, store: .standard))
@ObservationIgnored
public var lastViewed: Date?

The error message of the above is '@ObservableUserDefault' unable to extract the required values from the argument

Thanks for the macro!

How to work with [String] and [String]?

Is there a trick to getting this to work with an array? At the moment I simply using the following method:

var favorites: [String] { get { access(keyPath: \.favorites) return UserDefaults.standard.value(forKey: "favorites") as? [String] ?? [] } set { withMutation(keyPath: \.favorites) { UserDefaults.standard.set(newValue, forKey: "favorites") } } }

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.