You can use #function
literal to get the property name as UserDefault key. In the following example, #function
will get replaced by accessToken
. Be aware, changing the property names may break functionality for the existing users.
extension UserDefaults {
var accessToken: String? {
get {
return string(forKey: #function)
}
set {
set(newValue, forKey: #function)
synchronize()
}
}
}