title: Reset NSUserDefault key everyday in Swift
body: <p>I am writing an app which requires to reset a key stored in NSUserDefaults every day. (At 00:00 hours)</p>
  
  <p>I have achieved this but the method I used is a messy and unreliable one. </p>
  
  <p>Is there an easy way to achieve my goal? </p>
  
  <p>Here is the code. </p>
  
  <pre><code>    extension NSDate
      {
  
          func month() -&gt; Int
          {
              let calendar = NSCalendar.currentCalendar()
              let components = calendar.components(.Month, fromDate: self)
              let month = components.month
              return month
          }
  
  
          func date() -&gt; Int
          {
              let calendar = NSCalendar.currentCalendar()
              let components = calendar.components(.Day, fromDate: self)
              let date = components.day
  
              return date
          }
  
          func hour() -&gt; Int
          {
              //Get Hour
              let calendar = NSCalendar.currentCalendar()
              let components = calendar.components(.Hour, fromDate: self)
              let hour = components.hour
  
              //Return Hour
              return hour
          }
  
  
          func minute() -&gt; Int
          {
              //Get Minute
              let calendar = NSCalendar.currentCalendar()
              let components = calendar.components(.Minute, fromDate: self)
              let minute = components.minute
  
              //Return Minute
              return minute
          }
  
          func toShortTimeString() -&gt; String
          {
              //Get Short Time String
              let formatter = NSDateFormatter()
              formatter.timeStyle = .ShortStyle
              let timeString = formatter.stringFromDate(self)
  
              //Return Short Time String
              return timeString
          }
      }
  
  
  class ViewController: UIViewController {
  
  
      var totalcounterme:Int! //stored variable
      let currentDate = NSDate() //current date
      var todaysDate:Int! //date variable for storing local value
  
  
  
  
      @IBOutlet weak var todayTotal: UILabel! //total cigarrete count value
  
      @IBOutlet weak var numberC: UITextField! //text label value
  
      @IBAction func countPressed(sender: AnyObject) {
          var counter = totalcounterme //add local value to the counter
          if numberC.text != "" {
          counter = counter + Int(numberC.text!)! //add input value to counter
          numberC.text = ""
          todayTotal.text = "\(counter)" //display value of counter
          NSUserDefaults.standardUserDefaults().setInteger(counter, forKey: "storedCounter") //store counter value
          totalcounterme = NSUserDefaults.standardUserDefaults().integerForKey("storedCounter") //return stored value
          todaysDate = currentDate.date()
              if todaysDate &gt; NSUserDefaults.standardUserDefaults().integerForKey("finaldate") {
                  NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "storedCounter")
  
              } else if todaysDate &lt; NSUserDefaults.standardUserDefaults().integerForKey("finaldate") {
                  if currentDate.month() &gt; NSUserDefaults.standardUserDefaults().integerForKey("finalmonth") {
                      NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "storedCounter")
                  }
              }
  
          NSUserDefaults.standardUserDefaults().setInteger(todaysDate, forKey: "finaldate") //set todays date in value
          NSUserDefaults.standardUserDefaults().setInteger(currentDate.month(), forKey: "finalmonth") //set month
  
          }
          }
  
  
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
  
          let month = NSUserDefaults.standardUserDefaults().integerForKey("finalmonth")
          let today = NSUserDefaults.standardUserDefaults().integerForKey("finaldate")
          if currentDate.date() &gt; today {
              NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "storedCounter")
          } else if currentDate.date() &lt; today {
              if currentDate.month() &gt; month {
              NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "storedCounter")
              }
          }
  
          totalcounterme = NSUserDefaults.standardUserDefaults().integerForKey("storedCounter")
  
          todayTotal.text = "\(totalcounterme)"
  
      }
  }
  </code></pre>