ios – How do you save the worth of an Int that’s incrementing?


I’ve this rely property and I wish to increment it and hold monitor of it even when the app is killed. At the moment the incrementation works, however once I shut the app and I begin tapping on the incrementNumber button it resets to 0. Why does that occur? Right here is the code I’ve at present:

class ViewController: UIViewController, CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate {

    let defaults = UserDefaults.normal
    var rely = 0

    @IBAction func incrementNumber(_ sender: Any) {
        //increment right here
        rely += 1
        print("counter is (rely)")
        if rely >= 4 {
            //do one thing right here
            defaults.set(rely + 1, forKey: "Key")


Leave a Reply

Your email address will not be published. Required fields are marked *