我正在编写一个应用程序,它需要每天重置存储在NSUserDefaults中的密钥。 (00:00时)
我已经实现了这一点,但我使用的方法是一个混乱且不可靠的方法。
有没有简单的方法来实现我的目标?
这是代码。
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 :(得分:3)
您可以使用getter和setter创建NSUserDefault扩展计算属性,以保存用户上次访问日期。 NSCalendar有一个名为isDateInToday的方法,如果上次访问日期与今天不同,您可以在setter内部使用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()
}