I'm using Swift 4.0 and I'm using UserDefaults
in my project for storing Strings like login or something. I have a class:
class Cache: UserDefaultsManager {
static var currentProfileID: Int? {
get {
return value(forKey: Constants.Cache.CurrentProfileID) as? Int
}
set {
set(newValue, forKey: Constants.Cache.CurrentProfileID)
}
}
static var login: String? {
get {
return value(forKey: Constants.Cache.Login) as? String
}
set {
set(newValue, forKey: Constants.Cache.Login)
}
}
// ... and there are more of this kind
, where UserDefaultsManager
looks like this:
class UserDefaultsManager {
private static var uds: UserDefaults {
return UserDefaults.standard
}
static func value(forKey: String) -> Any? {
if let encoded = uds.object(forKey: forKey) as? Data {
return NSKeyedUnarchiver.unarchiveObject(with: encoded)
}
return nil
}
static func set(_ data: Any?, forKey: String) {
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: data as AnyObject)
uds.set(encodedData, forKey: forKey)
if (!uds.synchronize()) {
NSLog("Failed sync UserDefaults?")
}
}
}
Constants.Cache
is a structure that holds my keys. My question is:
What would be a good approach to make my
Cache
class lighter with less code?
I imagine the final product should look like:
class Cache {
static var hash: UDSObject<String>?
static var currentProfileID: UDSObject<Int>?
static var login: UDSObject<String>?
static var selectedWindowID: UDSObject<Int>?
}