10 分钟后重置 Swift 应用程序

Posted

技术标签:

【中文标题】10 分钟后重置 Swift 应用程序【英文标题】:Resetting Swift App After 10 Minutes 【发布时间】:2016-01-01 03:22:06 【问题描述】:

我正在使用 Swift 制作一个小费计算器应用程序。我的目标是在 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() 被调用时,我所有代表 UILabel 和 UITextFields 的变量都变为 nil 并且出现错误

致命错误:在展开 Optional 值时意外发现 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)

【问题讨论】:

【参考方案1】:

您的问题是,当您说 ViewController().closeApp() 时,您正在分配 ViewController 的新实例并在该实例上调用 closeApp 函数。由于您没有从情节提要中实例化该实例,因此没有附加任何出口,您会得到一个 nil 引用异常。

您需要调用现有 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 中,只需将代码从视图控制器移动到应用程序委托方法,然后从viewWillAppear中的默认值重新加载

以上是关于10 分钟后重置 Swift 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

Shiro 在 2 分钟后重置会话

NavController 中的滚动视图在显示键盘后不会重置(Swift)

是啥导致发送 TCP/IP 重置 (RST) 标志?

在 docker 容器中几分钟后 Spring Boot 连接重置

Swift ios 重置视图控制器层次结构

Swift:动画后将 UIView 的帧重置为其约束