将计数用户默认值重置为来自扩展方法之外的函数。在这种情况下,它的功能很简单。现在代码从 0 开始,除非我可以使用 func 来阻止代码上升。
extension UIViewController {
func updateVisitCount() {
let key = String(describing: type(of: self))
let count = UserDefaults.standard.value(forKey: key) as? Int ?? 0
UserDefaults.standard.set(count + 1, forKey: key)
}}
class easy: UIViewController {
@IBAction func delete() {
//reset let count to 0
}}
最佳答案
再做一个函数就是这样
extension UIViewController {
func updateVisitCount() {
let key = String(describing: type(of: self))
let count = UserDefaults.standard.value(forKey: key) as? Int ?? 0
UserDefaults.standard.set(count + 1, forKey: key)
}
func clearVisitCount() {
let key = String(describing: type(of: self))
UserDefaults.standard.set(0, forKey: key)
}
}
class easy: UIViewController {
@IBAction func delete() {
//reset let count to 0
self.clearVisitCount()
}
}
关于swift - 从另一个类重置扩展文件 userdefault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53567297/