Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Defaultskit | 1,418 | 6 | 6 months ago | 8 | October 22, 2018 | 11 | mit | Swift | ||
Simple, Strongly Typed UserDefaults for iOS, macOS and tvOS | ||||||||||
Userdefaultsstore | 452 | a year ago | 18 | August 21, 2020 | mit | Swift | ||||
Why not use UserDefaults to store Codable objects 😉 | ||||||||||
Foil | 387 | 17 days ago | 4 | January 25, 2022 | 2 | mit | Swift | |||
A lightweight property wrapper for UserDefaults done right | ||||||||||
Persistencekit | 135 | 1 | 2 years ago | 10 | March 16, 2022 | mit | Swift | |||
Store and retrieve Codable objects to various persistence layers, in a couple lines of code! | ||||||||||
Stores | 112 | 2 months ago | 1 | mit | Swift | |||||
Typed key-value storage solution to store Codable types in various persistence layers with few lines of code! | ||||||||||
Storez | 65 | 1 | a year ago | 6 | March 30, 2019 | 1 | mit | Swift | ||
💾 Safe, statically-typed, store-agnostic key-value storage written in Swift! | ||||||||||
Swift Standard Clients | 30 | 2 years ago | mit | Swift | ||||||
Client declarations and live implementations for standard iOS managers | ||||||||||
Keyvaluecontainer | 9 | a year ago | 1 | January 22, 2020 | mit | Swift | ||||
Type-safe containers for key-value storage | ||||||||||
Objectuserdefaults | 6 | 5 years ago | mit | Swift | ||||||
An object oriented and clean wrapper UserDefaults. | ||||||||||
Userdefaults | 3 | 5 years ago | mit | Swift | ||||||
🗂 UserDefaults extension framework |
DefaultsKit leverages Swift 4's powerful Codable capabilities to provide a Simple and Strongly Typed wrapper on top of UserDefaults. It uses less than 70 lines of code to acomplish this.
Installation >> instructions
<<
Instantiate, or get a shared
instance of Defaults
let defaults = Defaults() // or Defaults.shared
Then:
// Define a key
let key = Key<String>("someKey")
// Set a value
defaults.set("Codable FTW 😃", for: key)
// Read the value back
defaults.get(for: key) // Output: Codable FTW 😃
if defaults.has(key) {
// Do your thing
}
If you just need to know that a key/value pair exists, without actually using the value, use the
has()
method instead of the optionalget(for:key)
. For complex objects it will prevent any unnecessary deserialization.
You can find a convenience wrapper for your keys by extending DefaultsKey
. This allows you use Implicit Member Expression:
// Extend with a custom key
extension DefaultsKey {
static let someKey = Key<String>("someKey")
}
// Then use it like this
defaults.set("Some key", for: .someKey)
defaults.get(for: .someKey) // Output: Some key
To store a complex object just conform to the Codable protocol:
struct Person: Codable {
let name: String
let age: Int
}
Then:
// Create a key
let key = Key<Person>("personKey")
// Get an instance of your Codable conforming enum, struct or class
let person = Person(name: "Bonnie Greenwell", age: 80)
// Set the value
defaults.set(person, for: key)
And finally:
// Read it back
let person = defaults.get(for: key)
person?.name // Bonnie Greenwell
person?.age // 80
You can also use nested objects as long as they conform to the Codable
protocol:
enum Pet: String, Codable {
case cat
case dog
}
struct Person: Codable {
let name: String
let pets: [Pet]
}
// Get a Codable conforming instante
let person = Person(name: "Claire", pets: [.cat])
// Set the value
defaults.set(person, for: key)
// And read it back
let person = defaults.get(for: key)
person?.name // Claire
person?.pets.first // cat
DefaultsKit is released under the MIT license. See LICENSE for details.
Chinese is the #1 spoken language in the world and I'd love to have DefaultsKit be more inclusive, unfortunately I don't speak Chinese. If you know chinese, and would like to help out, please see issue #1
Thank you 🙏