A key-value storage cache tool on iOS Platform. Like UserDefaults.


Build Status Swift4 compatible

A key-value storage cache tool like UserDefaults on iOS Platform.


Normal Usage

Like UserDefaults:

// Save cache.
let intVal = 123
try UserCaches.standard.set(intVal, forKey: key)
// Get cache. Specify the type('Int') to decide generic type.
let cache: Int = try UserCaches.standard.value(forKey: key)

UserCaches.standard is a global instance of UserCaches.

Could also create new instance:

let cacheHelper = try UserCaches(cachePath: URL(fileURLWithPath: "/tmp/usercache.db"))

Advance Usage

However, I suggest using following usage. (Example of setting of Baidu App)

import UserCaches

struct User: Codable {
    let id: Int64
    let name: String

enum ComBaiduMobileUserSetting: String, UserCachesSettable {
    /// 隐私设置 - 允许通过手机号搜索到我
    case privacy_findMeByPhoneNumber
    /// 隐私设置 - 可通过感兴趣的人找到我
    case privacy_findMeByInteresting
    /// 隐私设置 - 开启通讯录关联
    case privacy_relateAddressList
    /// 隐私设置 - 黑名单
    case privacy_blacklist
    /// 字体大小
    case font_size
    var identifierMode: CacheKeyMode { return .identifier }
// Save caches = true = false   = false = [CacheCodability(User(id: 100120054,
                                                                            name: "abc"))] = 20

// Get caches
let isFindMeByPhoneNumber: Bool? = ComBaiduMobileUserSetting.privacy_findMeByPhoneNumber.value()
let isFindMeByInteresting: Bool? = ComBaiduMobileUserSetting.privacy_findMeByInteresting.value()
let isRelateAddressList: Bool? = ComBaiduMobileUserSetting.privacy_relateAddressList.value()
let blacklist: [CacheCodability<User>] = ComBaiduMobileUserSetting.privacy_blacklist.value()
let fontSize: Int = ComBaiduMobileUserSetting.font_size.value() ?? 16

If you set identifierMode to CacheKeyMode.identifier, UserCachesSettable will translate upper-case letter to lower-case letter and insert "." to translated left side on enum-name and replace "_" with "." on enum-case.

As above:

"ComBaiduMobileUserSetting" => ""

"privacy_findMeByPhoneNumber" => "privacy.findMeByPhoneNumber"

The case privacy_findMeByPhoneNumber is translated to, as a key, associated with true for this example.

Default Support Type

UseCaches support by default:

Default Support Type
Int, Int64, UInt, Uint64
Float, Double
String, Data
Date (Implement with TimeInterval)
Dictionary<Key: CacheCodable, Value: CacheCodable>

Especially, if a struct (or class) defer to Codable, use CacheCodability wrap the struct (or class), UserCaches also accept it. See above CacheCodability<User>


Note: UserCaches requires Swift 4.1 and Xcode 9.3+


CocoaPods is a dependency manager for Cocoa projects. To install UserCaches with CocoaPods:

  1. Make sure CocoaPods is installed. (UserCaches requires version 1.0.0 or greater.)

    # Using the default Ruby install will require you to use sudo when
    # installing and updating gems.
    [sudo] gem install cocoapods
  2. Update your Podfile to include the following:

    target 'YourAppTargetName' do
        pod 'UserCaches', '~> 0.0.5'
  3. Run pod install --repo-update.

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code.

  1. Add the following to your Package.swift file:
dependencies: [
    .package(url: "", from: "0.0.3")
  1. Build your project:
$ swift build

How to Test

  1. clone this repo.
  2. cd the repo directory.
  3. Execute orderswift test.




UserCaches is available under the MIT license. 


  • Swift Tools 4.0.0
View More Packages from this Author


Last updated: Mon Mar 13 2023 04:18:28 GMT-0500 (GMT-05:00)