maniramezan / userdefaultmacro Goto Github PK
View Code? Open in Web Editor NEWSeveral macros can be used to reduce boilerplate code to work with `UserDefaults` as a storage
License: MIT License
Several macros can be used to reduce boilerplate code to work with `UserDefaults` as a storage
License: MIT License
First of all, please note that I am not very good at English and am using machine translation.
It is great to see that this repository and UserDefaults can be handled easily. I know this is not the purpose of this repository, but I would like to request KeyValueStore support.
For example, we hope to be able to work like this.
@KVSModel(.cloud)
class Settings {
@LocalSaveProperty(originalName: "otherValue_1")
var value_1: Bool = false
var value_2: Bool = false
var value_3: Bool = false
@CloudSaveProperty(originalName: "otherValue_2")
var value_4: Bool = false
}
After deploying @KVSModel
class Settings {
@LocalSaveProperty(originalName: "otherValue_1")
var value_1: Bool = false
@CloudSaveProperty
var value_2: Bool = false
@CloudSaveProperty
var value_3: Bool = false
@CloudSaveProperty(originalName: "otherValue_2")
var value_4: Bool = false
private let userDefaults: UserDefaults
private let keyValuseStore: NSUbiquitousKeyValueStore
internal init(userDefaults: UserDefaults = .standard, keyValueStore: NSUbiquitousKeyValueStore = .default) {
self.userDefaults = userDefaults
self.keyValuseStore = keyValuseStore
}
}
Final output code.
class Settings {
var value_1: Bool = false {
get {
userDefaults.bool(forKey: "otherValue_1")
}
set {
userDefaults.set(newValue, forKey: "otherValue_1")
}
}
var value_2: Bool = false {
get {
keyValuseStore.bool(forKey: "value_2")
}
set {
keyValuseStore.set(newValue, forKey: "value_2")
}
}
var value_3: Bool = false {
get {
keyValuseStore.bool(forKey: "value_3")
}
set {
keyValuseStore.set(newValue, forKey: "value_3")
}
}
var value_4: Bool = false {
get {
keyValuseStore.bool(forKey: "otherValue_2")
}
set {
keyValuseStore.set(newValue, forKey: "otherValue_2")
}
}
private let userDefaults: UserDefaults
private let keyValuseStore: NSUbiquitousKeyValueStore
internal init(userDefaults: UserDefaults = .standard, keyValueStore: NSUbiquitousKeyValueStore = .default) {
self.userDefaults = userDefaults
self.keyValuseStore = keyValuseStore
}
}
Please note that this was written lightly and any errors are to be expected. Also, default values need to be considered.
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.