I am beginner with the Swift
having no advance knowledge with operators.
I have the following class
class Container {
var list: [Any]
Static function with generic type
class Defaults {
static subscript(key: DefaultsKey) -> T? {
get {
return UserDefaults.standard.value(forKey: key.rawValue) as? T
}
set {
UserDefaults.standard.set(newValue, forKey: key.rawValue)
}
}
}
enum DefaultsKey: String {
case firstKey = "JSGHDjasgchvsas"
}
Save value
Defaults[.firstKey] = 34
Get value
let value: Int = Defaults[.firstKey]
Happy coding!