I'm Andrew, software developer.
I work in London at Symplectic.
On the side I'm working on an iOS app Reading List.
A Swift library to enable easy persistent UserDefaults storage
License: MIT License
I'm Andrew, software developer.
I work in London at Symplectic.
On the side I'm working on an iOS app Reading List.
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.
If you change type definition of Codable instance, then you get crash immediately by force try.
Maybe Codable is not fully supported?
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)
}
}
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.