2016-01-27 79 views
0

我在寫一個應用程序,它需要每天重置存儲在NSUserDefaults中的密鑰。 (在00:00時)在Swift中每天重置NSUserDefault鍵

我已經達到了這個目標,但是我使用的方法是混亂而不可靠的。

有沒有簡單的方法來實現我的目標?

這是代碼。

extension NSDate 
    { 

     func month() -> Int 
     { 
      let calendar = NSCalendar.currentCalendar() 
      let components = calendar.components(.Month, fromDate: self) 
      let month = components.month 
      return month 
     } 


     func date() -> Int 
     { 
      let calendar = NSCalendar.currentCalendar() 
      let components = calendar.components(.Day, fromDate: self) 
      let date = components.day 

      return date 
     } 

     func hour() -> Int 
     { 
      //Get Hour 
      let calendar = NSCalendar.currentCalendar() 
      let components = calendar.components(.Hour, fromDate: self) 
      let hour = components.hour 

      //Return Hour 
      return hour 
     } 


     func minute() -> Int 
     { 
      //Get Minute 
      let calendar = NSCalendar.currentCalendar() 
      let components = calendar.components(.Minute, fromDate: self) 
      let minute = components.minute 

      //Return Minute 
      return minute 
     } 

     func toShortTimeString() -> 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 > NSUserDefaults.standardUserDefaults().integerForKey("finaldate") { 
       NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "storedCounter") 

      } else if todaysDate < NSUserDefaults.standardUserDefaults().integerForKey("finaldate") { 
       if currentDate.month() > 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() > today { 
      NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "storedCounter") 
     } else if currentDate.date() < today { 
      if currentDate.month() > month { 
      NSUserDefaults.standardUserDefaults().setInteger(0, forKey: "storedCounter") 
      } 
     } 

     totalcounterme = NSUserDefaults.standardUserDefaults().integerForKey("storedCounter") 

     todayTotal.text = "\(totalcounterme)" 

    } 
} 
+0

請發佈您目前所擁有的,這樣我們可以最好地幫助決定如何改進。 – Otra

+0

這是一個懶惰的代碼。但無論如何,我會暫時放下它。 @Odow – Sanidhay

+3

問題是你試圖完成你的任務的方式。午夜沒有必要這樣做。只需保存您保存設置的日期,並在下次用戶打開應用程序時檢查該日期是否與當天一樣,否則將其重置 –

回答

3

您可以使用getter和setter創建NSUserDefault擴展計算屬性以保存用戶上次訪問日期。 NSCalendar有一個名爲isDateInToday,你可以用你的二傳手裏面removePersistentDomainForName如果最後訪問日期不在同一天,今天方法:

extension UserDefaults { 

    static let defaults = UserDefaults.standard 

    static var lastAccessDate: Date? { 
     get { 
      return defaults.object(forKey: "lastAccessDate") as? Date 
     } 
     set { 
      guard let newValue = newValue else { return } 
      guard let lastAccessDate = lastAccessDate else { 
       defaults.set(newValue, forKey: "lastAccessDate") 
       return 
      } 
      if !Calendar.current.isDateInToday(lastAccessDate) { 
       print("remove Persistent Domain") 
       UserDefaults.reset() 
      } 
      defaults.set(newValue, forKey: "lastAccessDate") 
     } 
    } 

    static func reset() { 
     defaults.removePersistentDomain(forName: Bundle.main.bundleIdentifier ?? "") 
    } 
} 

只需設置你的用戶的最後訪問日期當用戶打開您的應用程序:

override func viewDidLoad() { 
    super.viewDidLoad() 
    UserDefaults.lastAccessDate = Date() 
} 
+0

請您解釋如何檢查日期是否已更改? – Sanidhay

+0

每次在setter中設置日期時都有一個條件如果不是(!)日期在sameDayAsToday中,則刪除持久域 –