Skip to content

Instantly share code, notes, and snippets.

@tonyarnold
Last active September 21, 2017 23:49
Show Gist options
  • Save tonyarnold/7de7f5df61d187f4b35760c7b53a6f6f to your computer and use it in GitHub Desktop.
Save tonyarnold/7de7f5df61d187f4b35760c7b53a6f6f to your computer and use it in GitHub Desktop.
Adds subscripting to `Foundation.UserDefaults`, and defines a type-safe key type
//
// Copyright © 2017 Tony Arnold.
//
// This code is licensed under the MIT license.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import Foundation
public extension UserDefaults {
public struct Key: RawRepresentable, Equatable, Hashable {
public private(set) var rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
public var hashValue: Int {
return rawValue.hashValue
}
public static func == (lhs: Key, rhs: Key) -> Bool {
return lhs.rawValue == rhs.rawValue
}
}
public subscript<T>(key: UserDefaults.Key) -> T? {
get { return value(forKey: key.rawValue) as? T }
set { set(newValue, forKey: key.rawValue) }
}
public subscript<T>(key: String) -> T? {
get { return value(forKey: key) as? T }
set { set(newValue, forKey: key) }
}
public subscript(key: UserDefaults.Key) -> Int {
get { return integer(forKey: key.rawValue) }
set { set(newValue, forKey: key.rawValue) }
}
public subscript(key: UserDefaults.Key) -> Float {
get { return float(forKey: key.rawValue) }
set { set(newValue, forKey: key.rawValue) }
}
public subscript(key: UserDefaults.Key) -> Double {
get { return double(forKey: key.rawValue) }
set { set(newValue, forKey: key.rawValue) }
}
public subscript(key: UserDefaults.Key) -> Bool {
get { return bool(forKey: key.rawValue) }
set { set(newValue, forKey: key.rawValue) }
}
// You could also wrap archived types, like so:
public subscript(key: UserDefaults.Key) -> NSColor? {
get {
guard
let data = data(forKey: key.rawValue),
let color = NSUnarchiver.unarchiveObject(with: data) as? NSColor
else {
return nil
}
return color
}
set {
if let color = newValue {
let data = NSArchiver.archivedData(withRootObject: color)
set(data, forKey: key.rawValue)
} else {
removeObject(forKey: key.rawValue)
}
}
}
// If you want to, you could still define subscripts for Strings as keys. I won't judge you.
//
// Much.
subscript<T>(key: String) -> T? {
get { return value(forKey: key) as? T }
set { set(newValue, forKey: key) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment