我在寫一個應用程序,它需要每天重置存儲在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)"
}
}
請發佈您目前所擁有的,這樣我們可以最好地幫助決定如何改進。 – Otra
這是一個懶惰的代碼。但無論如何,我會暫時放下它。 @Odow – Sanidhay
問題是你試圖完成你的任務的方式。午夜沒有必要這樣做。只需保存您保存設置的日期,並在下次用戶打開應用程序時檢查該日期是否與當天一樣,否則將其重置 –