You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 ClassStruct and Enum are available for custom types.(Of course, subclasses of NSObject are available.)
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.
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
letuserDefaults=PersistentStore<CameraConfig>()
// Make an instance of CameraConfig
varcs=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 InMemoryStoreAnyStore 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)
}
}