TypedDefaults alternatives and similar libraries
Based on the "Database" category.
Alternatively, view TypedDefaults alternatives based on common mentions on social networks and blogs.
-
MMKV
An efficient, small mobile key-value storage framework developed by WeChat. Works on Android, iOS, macOS, Windows, POSIX, and OHOS. -
YapDatabase
YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers. -
ParseAlternatives
DISCONTINUED. GraphQLite is a toolkit to work with GraphQL servers easily. It also provides several other features to make life easier during iOS application development. [Moved to: https://github.com/relatedcode/GraphQLite] -
Unrealm
Unrealm is an extension on RealmCocoa, which enables Swift native types to be saved in Realm. -
Prephirences
Prephirences is a Swift library that provides useful protocols and convenience methods to manage application preferences, configurations and app-state. UserDefaults -
PredicateEditor
A GUI for dynamically creating NSPredicates at runtime to query data in your iOS app. -
realm-cocoa-converter
A library that provides the ability to import/export Realm files from a variety of data container formats. -
SecureDefaults
Elevate the security of your UserDefaults with this lightweight wrapper that adds a layer of AES-256 encryption -
MySQL
A stand-alone Swift wrapper around the MySQL client library, enabling access to MySQL servers. -
PersistenceKit
Store and retrieve Codable objects to various persistence layers, in a couple lines of code! -
PersistentStorageSerializable
Swift library that makes easier to serialize the user's preferences (app's settings) with system User Defaults or Property List file on disk. -
PostgreSQL
A stand-alone Swift wrapper around the libpq client library, enabling access to PostgreSQL servers. -
MongoDB
A stand-alone Swift wrapper around the mongo-c client library, enabling access to MongoDB servers. -
ObjectiveRocks
An Objective-C wrapper for RocksDB - A Persistent Key-Value Store for Flash and RAM Storage. -
FileMaker
A stand-alone Swift wrapper around the FileMaker XML Web publishing interface, enabling access to FileMaker servers.
CodeRabbit: AI Code Reviews for Developers

* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of TypedDefaults or a related project?
README
TypedDefaults
TypedDefaults
is a utility library to type-safely use NSUserDefaults.
Motivation
The talk Keep Calm and Type Erase On by Gwendolyn Weston at try! Swift 2016 is great and it inspired me to apply the technique Type Erasure for actual cases in app development.
Installation
- Install with CocoaPods
use_frameworks!
platform :ios, '8.0'
pod 'TypedDefaults'
Requirements
- iOS 8.0+
- Swift 4.0
- Xcode 9
Features
- Custom types can be type-safely stored in NSUserDefaults
- Dependency Injection support
Usage
Custom types
Custom types can be type-safely stored in NSUserDefaults.
Custom types only need to adopt DefaultsConvertible
protocol as described later. No need to inherit NSObject.
Therefore, Swift native Class
Struct
and Enum
are available for custom types.(Of course, subclasses of NSObject are available.)
DefaultsConvertible
protocol
public protocol DefaultConvertible {
static var key: String { get }
init?(_ object: Any)
func serialize() -> Any
}
Custom types are stored in NSUserDefaults as AnyObject.
serialize()
is called when saving and init?(_ object:)
is called when getting from NSUserDefaults.
It's assuemd each custom type and one configuration used in app is one-to-one relation.
Therefore, key
is prepared as type property in order to assign key
to one custom type
.
Example of custom type
This is an example of the custom type with flag for saving photo to CameraRoll
and Photo Size
as camera configuration.
struct CameraConfig: DefaultConvertible {
enum Size: Int {
case Large, Medium, Small
}
var saveToCameraRoll: Bool
var size: Size
// MARK: DefaultConvertible
static let key = "CameraConfig"
init?(_ object: Any) {
guard let dict = object as? [String: Any] else {
return nil
}
self.saveToCameraRoll = dict["cameraRoll"] as? Bool ?? true
if let rawSize = dict["size"] as? Int,
let size = Size(rawValue: rawSize) {
self.size = size
} else {
self.size = .Medium
}
}
func serialize() -> Any {
return ["cameraRoll": saveToCameraRoll, "size": size.rawValue]
}
}
Saving custom type to NSUserDefaults
PersistentStore
is the class to save custom types to NSUserDefaults.
Below is the sample of how to use it.
/// Specify a custom type when initializing PersistentStore
let userDefaults = PersistentStore<CameraConfig>()
// Make an instance of CameraConfig
var cs = CameraConfig([:])!
// Set
userDefaults.set(cs)
// Get
userDefaults.get()?.size // Medium
/// Change the size
cs.size = .Large
// Set
userDefaults.set(cs)
// Get
userDefaults.get()?.size // Large
Dependency Injection support
NSuserDefaults is not Unit Test friendly because it persistently stores data on file system.
TypedDefaults
has the types InMemoryStore
AnyStore
for Dependency Injection in order to test types which behave differently depending on custom types stored in NSuserDefaults.
InMemoryStore
adopts DefaultStoreType
protocol as well as PersistentStore
.
However, InMemoryStore
retains custom types only on memory, which is different from PersistentStore
.
As for AnyStore
, it is the type to abstract PersistentStore
and InMemoryStore
.
Example
This is the example to use InMemoryStore
and AnyStore
instead of PersistentStore
at Unit Test.
There is a class called CameraViewController
which inherits UIViewController.
It has a property config
to retain a custom type saved in NSuserDefaults. To support Dependency Injection, set AnyStore
as the type of config
.
class CameraViewController: UIViewController {
lazy var config: AnyStore<CameraConfig> = {
let ds = PersistentStore<CameraConfig>()
return AnyStore(ds)
}()
...
}
Because the type of config
is not PersistentStore
but AnyStore
, it can be replaced with InMemoryStore
at Unit Test as below.
class CameraViewControllerTests: XCTestCase {
var viewController: CameraViewController!
override func setUp() {
viewController = CameraViewController()
let defaultConfig = CameraConfig([:])!
let ds = InMemoryStore<CameraConfig>()
ds.set(defaultConfig) //
viewController.config = AnyStore(ds)
}
}
Release Notes
See https://github.com/tasanobu/TypedDefaults/releases
License
TypedDefaults
is released under the MIT license. See LICENSE for details.
*Note that all licence references and agreements mentioned in the TypedDefaults README section above
are relevant to that project's source code only.