TypesafeUserDefaults

master

A type safe extension to UserDefaults
kernandreas/TypesafeUserDefaults

Typesafe UserDefaults

Build Status

TypesafeUserDefaults is a thin extension to UserDefaults that enables a type safe use. As a bonus, types that conform to Codable can be stored in the UserDefaults.

Features

Strongly Typed

Define the keys:

enum UserKeys {
    static let name = UserDefaults.Key<String>("name")
    static let date = UserDefaults.Key<Date>("date")
}

Use them like the classic UserDefaults API:

UserDefaults.standard.set("Max Mustermann", forKey: UserKeys.name)
UserDefaults.standard.set(Date(), forKey: UserKeys.date)

let name = UserDefaults.standard.value(forKey: UserKeys.name)
let date = UserDefaults.standard.value(forKey: UserKeys.date)

Or use the convenient subscript:

UserDefaults.standard[UserKeys.name] = "Max Mustermann"
UserDefaults.standard[UserKeys.date] = Date()

let name = UserDefaults.standard[UserKeys.name]
let date = UserDefaults.standard[UserKeys.date]

The compiler will tell you if something is wrong:

// UIImage is not archivable in UserDefaults
let imageKey = UserDefaults.Key<UIImage>("image")

// Assigning value of type Date to a key that defined the value to be String
UserDefaults.standard[UserKeys.name] = Date()

// The return type does not match the type of the value
let date: Date = UserDefaults.standard[UserKeys.name]

Provide Default Values

Providing default values is simple as you define it in the same place with the key. No more need for using register

let pointsKey = UserDefaults.DefaultKey<Int>("points", 23)

var currentPoints = UserDefaults.standard[pointsKey] // 23
UserDefaults.standard[pointsKey] += 2
currentPoints = UserDefaults.standard[pointsKey] // 25

Store Codable Types

Conform to CustomDefaultsArchivable:

struct User: CustomDefaultsArchivable {
    let name: String
    let date: Date
}

Define the key:

let userKey = UserDefaults.Key<User>("user")

Use the key:

UserDefaults.standard[userKey] = User(name: "Max Mustermann", date: Date())
let name = UserDefaults.standard[userKey]?.name
let date = UserDefaults.standard[userKey]?.date

Requirements

>= Swift 4.1

Description

  • Swift Tools 4.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Nov 08 2022 14:55:58 GMT-0500 (GMT-05:00)