Ios 10分钟后重置Swift应用程序

Ios 10分钟后重置Swift应用程序,ios,swift,time,appdelegate,Ios,Swift,Time,Appdelegate,我正在使用Swift制作一个tip计算器应用程序。我的目标是在10分钟后重置我的应用程序(换句话说,将我的所有标签设置为$0.00,并在NSUserDefaults中设置默认值) 我将这3个函数放在我的ViewController.swift文件中: func compareTimes(opens: NSDate?, closes: NSDate?) { if(opens!.timeIntervalSinceReferenceDate-closes!.timeIntervalSinceR

我正在使用Swift制作一个tip计算器应用程序。我的目标是在10分钟后重置我的应用程序(换句话说,将我的所有标签设置为$0.00,并在
NSUserDefaults
中设置默认值)

我将这3个函数放在我的ViewController.swift文件中:

func compareTimes(opens: NSDate?, closes: NSDate?) {
    if(opens!.timeIntervalSinceReferenceDate-closes!.timeIntervalSinceReferenceDate>600) {
        reset()
    }
}
func openApp() {
    openingTime = NSDate()
    let defaults = NSUserDefaults.standardUserDefaults()
    closingTime = defaults.objectForKey(closeKey) as! NSDate?
    if (closingTime != nil) {
        compareTimes(openingTime, closes: closingTime)
    }
}

func closeApp() {
    closingTime = NSDate()
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(closingTime, forKey: closeKey)
}
在我的AppDelegate中,我调用以下两种方法:

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    ViewController().closeApp()
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    ViewController().openApp()
}
请注意,关闭应用程序时会记录时间,打开应用程序时也会记录时间。比较这些时间,如果10分钟过去,则调用
reset()

我的问题是当调用
reset()
时,表示UILabels和UITextFields的所有变量都变为零,并且我得到一个错误

致命错误:在展开可选值时意外发现nil

这里是我的
reset()
方法供参考:

func reset() {
    billField.text=""
    tipLabel.text = "+ "+formatter.stringFromNumber(0)!
    totalLabel.text = formatter.stringFromNumber(0)
    total2.text = formatter.stringFromNumber(0)
    total3.text = formatter.stringFromNumber(0)
    total4.text = formatter.stringFromNumber(0)

    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject("15", forKey: key1)
    defaults.setObject("18", forKey: key2)
    defaults.setObject("20", forKey: key3)
    defaults.synchronize()
    percentages.append(0.1)
    percentages.append(0.1)
    percentages.append(0.1)
    percentButton.setTitle("15%", forSegmentAtIndex: 0)
    percentButton.setTitle("18%", forSegmentAtIndex: 1)
    percentButton.setTitle("20%", forSegmentAtIndex: 2)
}

您的问题是,当您说
ViewController().closeApp()
时,您正在分配
ViewController
的新实例,并对该实例调用
closeApp
函数。由于您没有从故事板实例化该实例,因此没有附加任何出口,并且您会得到一个nil reference异常

您需要调用现有ViewController实例上的方法。您可以使用以下内容:

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    if let viewController = application.keyWindow?.rootViewController as? ViewController {
        viewController.closeApp()
    }
}


func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    if let viewController = application.keyWindow?.rootViewController as? ViewController {
        viewController.closeApp()
    }
}

那么如何从故事板实例化实例呢?我尝试了你的建议,但现在它根本没有调用closeApp()和openApp()方法。这仍然对你没有帮助-你需要屏幕上的实例。如果我的代码没有运行,则可能意味着条件向下转换失败,因此当前视图控制器不是ViewController。设置一个断点,看看发生了什么。或者,由于您要在NSUserDefaults中保存所有值,只需将代码从view controller移动到app delegate方法,然后在
ViewWillDisplay中从默认值重新加载即可