A property wrapper to save Codable
objects in UserDefaults
.
Inspired by https://www.swiftbysundell.com/articles/property-wrappers-in-swift/.
We only support Swift Package Manager
.package(url: "https://github.com/mrgrauel/UserDefaultsBacked.git", from: "1.0.0")
@UserDefaultsBacked("sample", defaultValue: false)
var sample: Bool
@UserDefaultsBacked("sampleString", defaultValue: "sample")
var sampleString: String
// MARK: `nil` Support
@UserDefaultsBacked("nil_check")
var nilSample: String?
// MARK: Custom `UserDefaults`
static var userDefaults = UserDefaults(suiteName: "StorageTests")!
@UserDefaultsBacked("other_defaults", defaultValue: true, userDefaults: userDefaults)
var otherDefaults: Bool
// MARK: Custom `Codable`
enum Environment: String, Codable {
case development, stage, production
}
@UserDefaultsBacked("environment", defaultValue: .production)
var environment: Environment
struct Mock: Codable {
let value: Int
}
@UserDefaultsBacked("codable")
var codable: Mock?