I have a class which has 2 sets of getter & setters. 1 set is the traditional type. These work as expected:
var userEmail : String
{
get {
return UserDefaults.standard.string(forKey: UserDefaultKeys.profileEmail) ?? ""
}
set (newValue) {
UserDefaults.standard.set(newValue, forKey: UserDefaultKeys.profileEmail)
UserDefaults.standard.synchronize()
}
}
var podSerialNum : Int
{
get {
return UserDefaults.standard.integer(forKey: UserDefaultKeys.profilePodSN)
}
set (newValue) {
UserDefaults.standard.set(newValue, forKey: UserDefaultKeys.profilePodSN)
UserDefaults.standard.synchronize()
}
}
var podID : Int
{
get {
return UserDefaults.standard.integer(forKey: UserDefaultKeys.profilePodID)
}
set (newValue) {
UserDefaults.standard.set(newValue, forKey: UserDefaultKeys.profilePodID)
UserDefaults.standard.synchronize()
}
}
The other gets/sets all properties at once.
var userProfile: (email:String?, podSn:String?, podId:String?)
{
get {
return ( UserDefaults.standard.value(forKey: UserDefaultKeys.profileEmail) as? String,
UserDefaults.standard.value(forKey: UserDefaultKeys.profilePodSN) as? String,
UserDefaults.standard.value(forKey: UserDefaultKeys.profilePodID) as? String)
}
set {
UserDefaults.standard.set(newValue.email, forKey: UserDefaultKeys.profileEmail)
UserDefaults.standard.set(newValue.podSn, forKey: UserDefaultKeys.profilePodSN)
UserDefaults.standard.set(newValue.podId, forKey: UserDefaultKeys.profilePodID)
UserDefaults.standard.synchronize()
}
}
userProfile.podSn always returns nil. However userProfile.email & userProfile.podId work as expected. Also, the direct getter (podSerialNum) works.
What am I missing?
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)