With a strictly typed language like Swift, you'd like to make everything as strictly typed as possible. Here, this is a simple hack for UserDefaults.
import TypedUserDefaults
extension UserDefaults.Key {
static let foo = UserDefaults.DefaultKey<Int>("foo", default: 1)
static let bar = UserDefaults.OptionalKey<String>("bar")
}
print("foo == \(UserDefaults.standard[.foo])")
UserDefaults.standard[.foo] += 1
if let bar = UserDefaults.standard[.bar] {
print("bar == \(bar)")
}
UserDefaults.standard[.bar] = "blah"
You can install TypedUserDefaults via Carthage by adding the following line to your Cartfile
:
github "nh7a/TypeUserDefaults"
Or maybe via Swift Package Manager by adding the following line to the dependencies
value of your Package.swift
.
dependencies: [
.Package(url: "https://github.com/nh7a/TypedUserDefaults.git", majorVersion: 1)
]
- Xcode 9.0
- Swift 4.0
Attributed is free software, and may be redistributed under the terms specified in the LICENSE file.