About CodableDefaults
CodableDefaults is a lightweight Swift library that provides a convenient way to persist Codable objects as UserDefaults. With CodableDefaults, you can easily store and retrieve Codable objects without the need for manual serialization and deserialization.
Key Features
- Seamless integration with UserDefaults
- Automatic serialization and deserialization of Codable objects
- Supports all Codable types including nested structures and arrays
- Easy customization of storage keys and default values
- Automatic data migration when schema changes
- Extensively tested and well-documented codebase
Getting Started
To get started using CodableDefaults, follow these simple steps:
Step 1: Installing CodableDefaults
To install CodableDefaults, you can use Swift Package Manager or Cocoapods. Choose the method that best fits your project requirements.
Swift Package Manager:
dependencies: [
.package(url: "https://github.com/your-username/CodableDefaults.git", from: "1.0.0")
]
Cocoapods:
pod 'CodableDefaults', '~> 1.0'
Step 2: Import the Library
In your Swift file, import the CodableDefaults module:
import CodableDefaults
Step 3: Define Your Codable Structure
Create a Swift struct or class that conforms to the Codable protocol. CodableDefaults supports all Codable types.
For example:
struct User: Codable {
var name: String
var age: Int
}
Step 4: Save and Retrieve Codable Objects
Using CodableDefaults, you can easily save and retrieve Codable objects:
// Save a Codable object
let user = User(name: "John", age: 25)
Defaults[\.user] = user
// Retrieve a Codable object
if let storedUser: User = Defaults[\.user] {
// Use the retrieved object
}
The above code snippet demonstrates how to save a User object to UserDefaults with a specified key, and then retrieve it later.
Customization
CodableDefaults allows you to customize various aspects of storing Codable objects:
Custom Storage Key
By default, CodableDefaults uses the property name as the key for storing objects in UserDefaults. You can customize the storage key by conforming to the CodableDefaultsKey
protocol and override the codableKey
property:
struct User: CodableDefaultsKey {
var name: String
var age: Int
static var codableKey: String {
return "customKey"
}
}
Default Value
You can specify a default value for a Codable property by conforming to the CodableDefaultsDefaultValue
protocol and implementing the default
property:
struct User: CodableDefaultsDefaultValue {
var name: String
var age: Int {
return 18
}
}
Migration
CodableDefaults provides automatic data migration when the schema of the Codable object changes. You can override the CodableDefaultsMigration
protocol and implement the migrate()
method to handle migration from older versions:
struct User: CodableDefaultsMigration {
var name: String
var age: Int
static func migrate(from oldVersion: Int) {
if oldVersion < 2 {
// Perform migration logic here
}
}
}
Conclusion
CodableDefaults simplifies the process of persisting Codable objects in UserDefaults. Take advantage of automatic serialization and deserialization, customization options, and easy data migration. Start using CodableDefaults in your projects today!