bsm-project/Friendvatars/Settings.swift

27 lines
624 B
Swift
Raw Normal View History

2018-11-19 14:31:37 +01:00
import Foundation
final class Settings {
private enum Keys: String {
case user = "current_user"
}
static var currentUser: User? {
get {
guard let data = UserDefaults.standard.data(forKey: Keys.user.rawValue) else {
return nil
}
return try? JSONDecoder().decode(User.self, from: data)
}
set {
if let data = try? JSONEncoder().encode(newValue) {
UserDefaults.standard.set(data, forKey: Keys.user.rawValue)
} else {
UserDefaults.standard.removeObject(forKey: Keys.user.rawValue)
}
UserDefaults.standard.synchronize()
}
}
}