在Swift 3中,我声明了一个总是返回非零正值的计算属性。的属性被存储在UserDefaults(这意味着,这将是零首次应用运行):Swift:递归设置计算属性
var notificationInterval: TimeInterval {
get {
let interval = UserDefaults(suiteName: "groupName")?.double(forKey: "notificationInterval") as TimeInterval?
if interval == nil || interval! <= 0 {
notificationInterval = defaultInterval
return notificationInterval
} else {
return interval!
}
}
set {
UserDefaults(suiteName: "groupName")?.set(newValue, forKey: "notificationInterval")
}
}
在管线6和7:
notificationInterval = defaultInterval
return notificationInterval
我得到以下错误:
Attempting to access 'notificationInterval' within its own getter.
我明白这个错误,但我会如何设计这个不同呢?我正在有目的地访问该属性“在其自己的获取者内”。
避免显式检查'nil'然后强制解包。只需使用条件绑定。 – Alexander