davidsteppenbeck / observableuserdefault Goto Github PK
View Code? Open in Web Editor NEWA Swift macro that adds accessor blocks to properties in `Observable` classes that read and write values in `UserDefaults`.
License: MIT License
A Swift macro that adds accessor blocks to properties in `Observable` classes that read and write values in `UserDefaults`.
License: MIT License
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
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!
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") } } }
Hello,
This library misses a major functionality of “observability”, which is notifying of changes that occur to the observed value from different processes.
Cheers
Please, add support enums for macro
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.