20 Oct 2017

Default – Modern interface to UserDefaults + Codable support

Default is a library that extends what UserDefaults can do by providing extensions for saving custom objects that conform to Codable and also providing a new interface to UserDefaults described below, via the protocol DefaultStorable. You can use only the Codable support extensions or the DefaultStorable protocol extensions or both. (or none, thats cool too)

Why default?

This library has Storing keys and values in defaults the normal way is error prone because typing out the string value for a key every time leaves the possibility of mistyped keys and keeping track of which keys are used and what is currently stored in UserDefaults is somewhat hard. Defining objects specifically for storing in user defaults makes the job of keeping track of what is currently being stored in UserDefaults as simple as searching the project’s source code for instances that conform to DefaultStorable. Using objects specificaly for storing a set of data in UserDefaults allows settings for a certain piece of data to be logically grouped together.


  •  Read and write custom objects directly to UserDefaults that conform to Codable
  •  Provides an alternative API to UserDefaults with DefaultStorable

Dont see a feature you need?

Feel free to open an Issue requesting the feature you want or send over a pull request!


DefaultStorable – A better way of interacting with UserDefaults

Instead of manually adding key values to the key store or having to implement NSCoding manually and bloating up object code, you can simply and clearly define defaults objects with a clear intent of being used as a means of storing defaults.

Much like how conforming to Codable gets you a lot for free, so does conforming to DefaultStorable. **The object conforming to DefaultStorable must also conform to Codable:

Say we want to store theme settings in UserDefaults (fair enough right?) we first define our object conforming to Codable and DefaultStorable.

Define object conforming to DefaultStorable

struct VisualSettings: Codable, DefaultStorable {
    let themeName: String
    let backgroundImageURL: URL?

Create & Save the object to UserDefaults

let settings = VisualSettings(themeName: "bright", backgroundImageURL: URL(string: "https://..."))

Read it back later when ya need it!

if let settings = VisualSettings.fetchFromDefaults() {
    // Do something

Swift 4 Codable Support

This library offers support for directly storing custom objects to UserDefaults that conform to Codable. With the release of Swift 4 comes the Codable protocol, which provides support for serializing objects. UserDefaults has not been updated to work with Swift 4’s Codable protocol so if saving custom objects directly to UserDefaults is necessary then that object must support NSCoding and inherit from NSObject.

// 1: Declare object (just conform to Codable, getting default encoder / decoder implementation for free)
struct VolumeSetting: Codable {
    let sourceName: String
    let value: Double

let setting = VolumeSetting(sourceName: "Super Expensive Headphone Amp", value: 0.4)
let key: String = String(describing: VolumeSetting.self)

// 2: Write
UserDefaults.standard.df.store(setting, forKey: key)

// 3: Read
UserDefaults.standard.df.fetch(forKey: key, type: VolumeSetting.self)

Alternative to using Codable

// 1: Define class 
class VolumeSetting: NSObject, NSCoding {
    let sourceName: String
    let value: Double
    init(sourceName: String, value: Double) {
        self.sourceName = sourceName
        self.value = value
    required init(coder decoder: NSCoder) {
        self.sourceName = decoder.decodeObject(forKey: "sourceName") as? String ?? ""
        self.value = decoder.decodeDouble(forKey: "value")

    func encode(with coder: NSCoder) {
        coder.encode(sourceName, forKey: "sourceName")
        coder.encode(value, forKey: "value")

// 2: Write

let setting = VolumeSetting(sourceName: "Super Expensive Headphone Amp", value: 0.4)
let encodedData = NSKeyedArchiver.archivedData(withRootObject: setting)
UserDefaults.standard.set(encodedData, forKey: "volume")

// 3: Read

if let data = UserDefaults.standard.data(forKey: "volume"),
   let volumeSetting = NSKeyedUnarchiver.unarchiveObject(with: data) as? VolumeSetting {
   // do something



If you use Carthage to manage your dependencies, simply add Default to your Cartfile:

github "Nirma/Default"

If you use Carthage to build your dependencies, make sure you have added Default.framework to the “Linked Frameworks and Libraries” section of your target, and have included Default.framework in your Carthage framework copying build phase.


If you use CocoaPods to manage your dependencies, simply add Default to your Podfile:

pod 'Default'


  • Xcode 9.0
  • Swift 4.0+


Contributions are more than welcome!


Default is free software, and may be redistributed under the terms specified in the LICENSE file.


Source: https://github.com/Nirma/Default

Leave a Reply

%d bloggers like this: