Skip to content

Instantly share code, notes, and snippets.

@Reflejo
Last active August 29, 2015 14:14
Show Gist options
  • Save Reflejo/63c1c9e7ad4b0916ca7a to your computer and use it in GitHub Desktop.
Save Reflejo/63c1c9e7ad4b0916ca7a to your computer and use it in GitHub Desktop.
NSUserDefaults abstraction
//
// Storage.swift
//
// Created by Martin Conte Mac Donell (Reflejo@gmail.com)
//
import Foundation
/**
This struct is the base for Storage Keys. This way we enforce all keys to be in a container to avoid typos
when using strings that are evaluated at runtime.
*/
public struct StorageKey: RawRepresentable {
public var rawValue: String
public init(rawValue value: String) {
self.rawValue = value
}
public init(_ value: String) {
self.rawValue = value
}
}
/**
This struct serves as a wrapper to NSUserDefaults. The idea is to abstract the device storage in order to
make storing data more flexible and modular. All keys should be set on the struct above.
*/
public struct Storage {
/**
Returns the object associated with the first occurrence of the specified key or the defaultValue if the
key is not found.
:param: key A key from the StorageKey enum.
:param: defaultValue The default value that is going to be returned if the key is not found.
:returns: The object associated with the specified key, or defaultValue if the key was not found.
*/
public static func get<T>(key: StorageKey, defaultValue: T) -> T {
return NSUserDefaults.standardUserDefaults().objectForKey(key.rawValue) as? T ?? defaultValue
}
/**
Returns the object associated with the first occurrence of the specified key.
:param: key A key from the StorageKey enum.
:returns: The object associated with the specified key, or nil if the key was not found.
*/
public static func get<T>(key: StorageKey) -> T? {
return NSUserDefaults.standardUserDefaults().objectForKey(key.rawValue) as? T
}
/**
Sets the value of the specified default key in the standard application domain.
:param: value The object to store in the defaults database if nil the key will get removed.
:param: key The key (from the StorageKey enum) with which to associate with the value.
*/
public static func set(value: AnyObject?, forKey key: StorageKey) {
if value == nil {
return NSUserDefaults.standardUserDefaults().removeObjectForKey(key.rawValue)
}
NSUserDefaults.standardUserDefaults().setObject(value, forKey: key.rawValue)
}
/**
Writes any modifications to the persistent domains to disk and updates all unmodified persistent
domains to what is on disk.
*/
public static func sync() {
NSUserDefaults.standardUserDefaults().synchronize()
}
/**
Removes all keys and values stored in the application domain (all of them).
*/
public static func removeAll() {
let appDomain = NSBundle.mainBundle().bundleIdentifier!
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)
}
}
// MARK: - Example
extension StorageKey {
static let KeyA = StorageKey("A")
static let KeyB = StorageKey("B")
static let KeyC = StorageKey("C")
}
Storage.set(1, forKey: .KeyA)
println(Storage.get(.KeyA) as Int?)
println(Storage.get(.KeyB, defaultValue: 0))
Storage.set([1, 2, 3], forKey: .KeyB)
println(Storage.get(.KeyB) as [Int]?)
Storage.set(NSData(), forKey: .KeyC)
println(Storage.get(.KeyC) as NSData?)
// Output:
// Optional(1)
// 0
// Optional([1, 2, 3])
// Optional(<>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment