所以我正在使用Swift构建一个简单的iOS应用程序.我需要NSTimer在应用程序进入后台后杀死我,并在应用程序再次激活后创建一个新应用程序.
最初我的解决方案是在主控制器文件中创建NSTimerclass in 的计时器ViewDidLoad().这导致我的应用程序中的错误.
我想我需要applicationDidEnterBackground()在AppDelegate.swift中使用来杀死计时器.但我不确定如何做到这一点.我应该在AppDelegate.swift或主控制器中创建计时器类吗?我不知道Swift文件如何共享类.
我在网上搜索了解决方案,但这些帖子都太旧了,解决方案是用Objective-C编写的.
我绝对是初学者.所以我希望有人可以在Swift中解释它.
这是我的主控制器TodoTableViewController.swift中的代码:
import UIKit
import CoreData
class TodoTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {....
...
...
override func viewDidLoad() {
super.viewDidLoad()
var fetchRequest = NSFetchRequest(entityName: "Todo")
let sortDescriptor = NSSortDescriptor(key: "dayleft", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext {
fetchResultController = NSFetchedResultsController(fetchRequest:fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
var e: NSError?
var result = fetchResultController.performFetch(&e) …Run Code Online (Sandbox Code Playgroud) 例如,我正在尝试制作一些SKSpriteNode,它们只能持续10秒。我想为节点创建一个名为“bornTime”的自定义属性,以便在 update() if 中currentTime - bornTime > 10,该节点将被删除。