For Swift 5
You can use DefaultsKit in order to store key - value pairs.
First Create a key for specific value
import DefaultsKit
extension DefaultsKey {
static let soundKey = Key("userKey")
}
Call & Set it from anywhere
public var user:User? {
get {
if Defaults.shared.has(.userKey) {
return Defaults.shared.get(for: .userKey)
} else {
return nil
}
}
set {
if let val = newValue {
Defaults.shared.set(val, for: .userKey)
} else {
Defaults.shared.clear(.userKey)
}
}
}