bsm-project/Friendvatars/Settings.swift
2018-11-19 15:09:22 +01:00

27 lines
624 B
Swift

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()
}
}
}