Skip to content

sven-m/typed-keys-ios

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Introduction

This is a playground showing how hide UserDefaults behind a fully typed key-value storage API

UserDefaults

Using UserDefaults, one might normally do the following:

let numberOfCakesKey = "NumberOfCakes"
UserDefaults.standard.register(defaults: [numberOfCakesKey: 4])
let numCakes = UserDefaults.standard.object(forKey: numberOfCakesKey) as? Int

The main problem with the above code is that:

  • you have to cast everywhere you fetch the value
  • the type of the value stored under a key, which usually does not change, is not enforced

A better API

It would be much nicer if one were to be able to do the following:

struct MyCodableStruct: Codable
{
    var stuff: Int
}

enum Keys
{
    static let numberOfCakes = TypedKey<Int>(name: "NumberOfCakes")
    static let arrayOfCodableStructs = TypedKey<[MyCodableStruct]>(name: "AmazingStuff")
}

UserDefaults.standard[Keys.arrayOfCodableStructs] = [MyCodableStruct(stuff: 6), MyCodableStruct(stuff: 5)]
assert(UserDefaults.standard[Keys.arrayOfCodableStructs].count == 2)

About

Typesafe key-value API for wrapping UserDefaults

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages