Latest 0.1.3
License MIT
Platforms ios 10.0, requires ARC
Dependencies SwiftKeychainWrapper

Swift version

Type-Safe Swift API for Key-Value Store database.

SwiftKeyValueStore is an extention for UserDefaults and SwiftKeychainWrapper to provide simple, type-safe, expressive Swifty API with the benefits of static typing. Chose what type of the database you want to use – unencrypted UserDefaults or encrypted storage in KeyChain. Define your keys in one place, use value types easily, and get extra safety and convenient compile-time checks for free.

Version 0.1


There’s only three steps to using SwiftKeyValueStore:

Step 1: Chose Storage type UserDefaults. Use standard shared instance or create new instance.

var DefaultsKeyValueStore = UserDefaults.standard

Or encrypted storage in KeyChain. Use standard shared instance or create new instance.

var KeychainKeyValueStore = KeychainWrapper.standard

Step 2: Define your keys.

extension KeyValueStoreKeys {
    static let userName = KeyValueStoreKey<String>("UserNameKey")
    static let onboardingIsEnabled = KeyValueStoreKey<Bool>("OnboardingIsEnabledKey")

Step 3: Use it

//Set and get Keychain.
KeychainKeyValueStore[.userName] = "[email protected]"
let username = KeychainKeyValueStore[.userName]

//Set and get User defaults 
DefaultsKeyValueStore[.onboardingIsEnabled] = true

// Modify value types in place
DefaultsKeyValueStore[.launchCount] += 1

// Use and modify typed arrays
DefaultsKeyValueStore[.movies][0] += " Last Jedi"

// Works with types that conform Codable or NSCoding
DefaultsKeyValueStore[.color] = UIColor.white
DefaultsKeyValueStore[.color]?.whiteComponent // => 1.0

The convenient dot syntax is only available if you define your keys by extending KeyValueStoreKeys class. Or just pass the KeyValueStoreKey value in square brackets. Or use String to create key with specified ValutType or default Value.


Define your keys

Define your user keys for your own convinience:

let userKey = KeyValueStoreKey<User>("userKey")
let colorKey = "ColorKey".toKeyWith(type: UIColor)
let profilesKey = "ProfilesKey".toKeyWith(defaultValue: Array<Profile>())

Create a KeyValueStoreKey object, provide the type of the value you want to store and the key name in parentheses.
Or use String extension for your convinience to create KeyValueStoreKey from String

Create Instance of your store. You can use UserDefaults store or KeyChainWrapper store.

var KeychainKeyValueStore = KeychainWrapper.standard
var DefaultsKeyValueStore = UserDefaults.standard

Now use the your store to access those values:

// store in UserDefaults
DefaultsKeyValueStore[colorKey] = "red"
DefaultsKeyValueStore[colorKey] // =>, typed as UIColor?

// store securely in KeyChain
KeychainKeyValueStore[userKey] = User(firstName: "Yuriy", 
                                      lastName: "Gagarin") // struct User has to conform `Codable` protocol 

KeychainKeyValueStore[userKey] // => (firstName: "Yuriy", 
                               //     lastName: "Gagarin"), typed as User?

The compiler would not let you to set a wrong value type, and alwasy returns expected optional type.

Supported types

SwiftKeyValueStore supports all of the standard NSUserDefaults types, like strings, numbers, booleans, arrays and dictionaries. As well as any types the conforms Codable or NSCoding protocol


SwiftKeyValueStore support Codable. Just add Codable protcol conformance to your type, like:

struct User: Codable {
    let firstName: String
    let lastName: String

You’ve got Array support for free:

let users = KeyValueStoreKey<[User]>("users")


SwiftKeyValueStore support NSCoding. Just add NSCoding protcol conformance to your type and implement required methods:

class UserProfileView: UIView, NSCoding  {
    let userID: String

    init(frame: CGRect, id: String) {
        self.userID = id
        super.init(frame: frame)

    override func encode(with aCoder: NSCoder) {
        aCoder.encode(userID, forKey: "UserProfileView.Id")
        super.encode(with: aCoder)

    required init?(coder aDecoder: NSCoder) {
        guard let id = aDecoder.decodeObject(forKey: "UserProfileView.Id") as? String else { return nil }
        self.userID = id
        super.init(coder: aDecoder)

Default values

let counter = KeyValueStoreKey<Int>("counterKey", defaultValue: 0)
let user = KeyValueStoreKey<User>("token", defaultValue: User(firstName: "Anakin", 
                                                              lastName: "Skywalker"))

Remove all keys

To reset user defaults, use resetStorage method.


Shared user defaults

If you’re sharing your user defaults between different apps or an app and its extensions, you can create you onw instance of UserDefaults or KeyChainWrapper.

var CustomSharedDefaults = UserDefaults(suiteName: "")!



If you’re using CocoaPods, just add this line to your Podfile:

pod 'SwiftKeyValueStore'

Install by running this command in your terminal:

pod install

Then import the library in all files where you use it:

import SwiftKeyValueStore

SwiftKeyValueStore is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "SwiftKeyValueStore",
    "version": "0.1.3",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "summary": "Simple Key Value Store. Wrapper for UserDefaults and KeyChain. Written in Swift.",
    "description": "Use SwiftKeyValueStore api if you want type-safe, easy to use database to persist data across launches of your app.",
    "homepage": "",
    "authors": {
        "Yevgeniy Prokoshev": "[email protected]"
    "platforms": {
        "ios": "10.0"
    "source": {
        "git": "",
        "tag": "0.1.3"
    "requires_arc": true,
    "swift_version": "4.0",
    "cocoapods_version": ">= 1.5.0",
    "source_files": "Source/**/*.swift",
    "dependencies": {
        "SwiftKeychainWrapper": []

Pin It on Pinterest

Share This