Skip to content
Why not use UserDefaults to store Codable objects 😉
Branch: master
Clone or download
omaralbeik Merge pull request #35 from omaralbeik/swift5
Update Project to Swift 5.0
Latest commit 9399dcc Apr 5, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
Assets v0.4 (#9) Jul 12, 2018
Sources Update Project to Swift 5.0 Apr 3, 2019
Tests Add SingleUserDefaultsStore (#24) Sep 12, 2018
UserDefaultsStore.xcodeproj Update Project to Swift 5.0 Apr 3, 2019
.codecov.yml Update CI (#20) Jul 28, 2018
.gitignore Fix Codecov (#18) Jul 28, 2018
.swiftlint.yml v1.2.2 (#28) Sep 30, 2018
.travis.yml Update Project to Swift 5.0 Apr 3, 2019
Gemfile Add Gemfile Jul 1, 2018
LICENSE v0.2 Jul 1, 2018
Package.swift Add SPM support Jul 29, 2018 Update Nov 7, 2018 Fixes #23 Remove Hashable constraint for ID (#30) Oct 4, 2018
UserDefaultsStore.podspec Update Project to Swift 5.0 Apr 3, 2019

Build Status Test Coverage codebeat badge Platforms Cocoapods Carthage compatible Swift Package Manager compatible Swift Xcode MIT

A Turkish version of this document can be found here.


You love Swift's Codable protocol and use it everywhere, who doesn't! Here is an easy and very light way to store and retrieve -reasonable amount 😅- of Codable objects, in a couple lines of code!


CocoaPods (Recommended)

To integrate UserDefaultsStore into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'UserDefaultsStore'

To integrate UserDefaultsStore into your Xcode project using Carthage, specify it in your Cartfile:

github "omaralbeik/UserDefaultsStore" ~> 1.0
Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but UserDefaultsStore does support its use on supported platforms.

Once you have your Swift package set up, adding UserDefaultsStore as a dependency is as easy as adding it to the dependencies value of your Package.swift.

import PackageDescription
dependencies: [
    .package(url: "", from: "1.0.2")

Add the Sources folder to your Xcode project.


Let's say you have 2 structs; User and Laptop defined as bellow:

struct User: Codable {
    var id: Int
    var firstName: String
    var lastName: String
    var laptop: Laptop?
struct Laptop: Codable {
    var model: String
    var name: String

Here is how you store them in UserDefaultsStore:

1. Conform to the Identifiable protocol and set the idKey property

The Identifiable protocol lets UserDefaultsStore knows what is the unique id for each object.

struct User: Codable, Identifiable {
    static let idKey = \
struct Laptop: Codable, Identifiable {
    static let idKey = \Laptop.model

Notice how User uses Int for its id, while Laptop uses String, in fact the id can be any Hashable type. UserDefaults uses Swift keypaths to refer to properties without actually invoking them. Swift rocks 🤘

2. Create UserDefaults Stores

let usersStore = UserDefaultsStore<User>(uniqueIdentifier: "users")!
let laptopsStore = UserDefaultsStore<Laptop>(uniqueIdentifier: "laptops")!

3. Voilà, you're all set!

let macbook = Laptop(model: "A1278", name: "MacBook Pro")
let john = User(userId: 1, firstName: "John", lastName: "Appleseed", laptop: macbook)

// Save an object to a store

// Save an array of objects to a store
try![jane, steve, jessica])

// Get an object from store
let user = store.object(withId: 1)
let laptop = store.object(withId: "A1278")

// Get all objects in a store
let laptops = laptopsStore.allObjects()

// Check if store has an object
print(usersStore.hasObject(withId: 10)) // false

// Iterate over all objects in a store
laptopsStore.forEach { laptop in

// Delete an object from a store
usersStore.delete(withId: 1)

// Delete all objects in a store

// Know how many objects are stored in a store
let usersCount = usersStore.objectsCount

Looking to store a single item only?

Use SingleUserDefaultsStore, it enables storing and retrieving a single value of Int, Double, String, or any Codable type.

Note about using class instead of struct

At the moment, only final classes are supported, please take this into consideration before using the library.


  • iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 10.0+
  • Swift 4.2+


Special thanks to:


Icon made by freepik from


UserDefaultsStore is released under the MIT license. See LICENSE for more information.

You can’t perform that action at this time.