【问题标题】:Getting Swift runtime error: "Can't end BackgroundTask: no background task exists with identifier 1"获取 Swift 运行时错误:“无法结束 BackgroundTask:不存在标识符为 1 的后台任务”
【发布时间】:2020-02-27 13:42:57
【问题描述】:

我正在尝试实现Swift Programming in Easy Steps exercise 中第 5 章中的 TableView 示例。我已经检查并重新检查了示例代码(甚至下载并测试了实际的示例代码),但我仍然收到此运行时错误。有人知道为什么会这样吗?

2019-11-01 07:56:51.247052+0100 TableView_EasySteps[2067:39485] 不能 end BackgroundTask:不存在标识符为 1 的后台任务 (0x1),或者它可能已经结束。打破 UIApplicationEndBackgroundTaskError() 进行调试。

这里是 ViewController 代码:

import UIKit

class WebsitesTableViewController: UITableViewController {

    var websites:[[String]] = [
        ["Apple",           "https://www.apple.com"]   ,
        ["NY Times",        "https://www.nytimes.com"] ,
        ["DN",              "https://www.dn.se"]   ,
        ["NFL",             "https://www.nfl.com"]   ,
        ["Premier League",  "https://www.premierleague.com"]   ,
        ["The Guardian",    "https://www.theguardian.com"]
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // preserve selection between presentations
        self.clearsSelectionOnViewWillAppear = false
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return websites.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier")
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellIdentifier")
        }
        cell!.textLabel!.text = websites[indexPath.row][0]
        cell!.detailTextLabel!.text = websites[indexPath.row][1]

        return cell!
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let url = URL(string: websites[indexPath.row][1]) {
            UIApplication.shared.open(url)
        }
    }

    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            websites.remove(at: indexPath.row)
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }
}

【问题讨论】:

    标签: swift tableview ios13 xcode11.1


    【解决方案1】:

    如果您使用 MVC 标准,请检查 SceneDelegate.swift 文件并确保它是控制文件的一部分,否则只需确保它与 AppDelegate.swift 和 ViewController.swift 一起是 Xcode 项目中文件的一部分其他人。

    【讨论】:

    • 我也遇到了这个错误。我没有使用 SceneDelegate.swift,我什至不知道 MVC 是什么。你是说模型-视图-控制器框架吗?
    • 是的,我所说的 MVC 是指模型-视图-控制器框架。好的,但是您的 Xcode 项目中是否有任何名为 SceneDelegate.swift 的文件?如果是这样,那么我确定该文件中发生了更改。请检查并撤消更改,您会没事的。
    • 好的。我会去做。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2014-07-15
    • 2020-05-26
    • 1970-01-01
    • 2020-05-22
    相关资源
    最近更新 更多