Search code examples
swiftextension-methodsresetibactionuserdefaults

reset extension file userdefualt from another class


Reset count userdefualt to from a function outside of the extension method. In this case its func easy. Right now the code starts at 0 and only goes unless i can use a func to stop the code from going up.

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
}}

Solution

  • Make one more function thats it

    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()
      }
    }