【问题标题】:Swift: "Fatal error: newElements.underestimatedCount was an overestimate" - what does this error mean?斯威夫特:“致命错误:newElements.underestimatedCount 被高估了”——这个错误是什么意思?
【发布时间】:2019-04-12 11:14:54
【问题描述】:

我有一个 tableView,它的数据源是一个计算数组 allItems

var pendingItems: [Item] = []      
var loadingItems: [Item] = []      
var processedItems: [Item] = []

var allItems: [Item] {
        return processedItems + loadingItems + pendingItems
    }  

有时,在运行应用程序时,我会收到以下错误: 线程 1:致命错误:newElements.underestimatedCount 被高估了

当我尝试通过此函数中的索引到达元素时,似乎会发生这种情况:

    func getCellContent(at index: Int) -> (url: String, status: String) {
        return (url: queue.allItems[index].url, status: queue.allItems[index].status.description)
    }

这是截图:https://www.dropbox.com/s/b9miuyiz1em56mk/Screen%20Shot%202019-04-12%20at%202.06.25%20PM.png?dl=1

有人能解释为什么会这样吗?非常感谢任何帮助!

数据源方法(来自视图控制器):

extension WebSearchViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return presenter.getNumberOfRows()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "WebPageTableViewCell", for: indexPath) as! WebPageTableViewCell
        let (url, status) = presenter.getCellContent(at: indexPath.row)
        cell.addressLabel.text = url
        cell.statusLabel.text = status
        return cell
    }
}

主持人的帮助方法:

    func getNumberOfRows() -> Int {
        return queue.allItems.count
    }

    func getCellContent(at index: Int) -> (url: String, status: String) {
        return (url: queue.allItems[index].url, status: queue.allItems[index].status.description)
    }

这是我的Item

class WebPage: NSObject {
    var url: String
    var status: URLStatus

    init(url: String, status: URLStatus = .unchecked) {
        self.url = url
        self.status = status
    }

    func changeStatus(to newStatus: URLStatus) {
        self.status = newStatus
    }

    static func == (lhs: WebPage, rhs: WebPage) -> Bool {
        return lhs.url == rhs.url
    }
}

【问题讨论】:

  • 每次调用 allItems 时,都会通过组合 3 个数组来创建一个新数组,至少在 getCellContent 中使用局部变量不是更好吗? let all = allItems
  • 添加 celforrow 和 numberofrows 代表代码
  • @dahiya_boy,我添加了数据源方法
  • @JoakimDanielson,我决定创建一个计算变量,因为它总是在变化。我从 pendingItems 中删除元素,将它们添加到 loadingItems 中,然后添加到 processedItems 中(一旦它们改变了它们的状态)。这就是为什么 allItems 数组总是不同的原因。我认为每当我需要一个特定的项目时,迭代一个巨大的数组是没有效率的。有没有更好的方法来组织不同状态的元素?
  • @AlinaVas 你能显示Item 模特吗?

标签: ios swift uitableview fatal-error


【解决方案1】:

您的代码绝对没问题,而且很难说出错误的根本原因。以下是关于underestimatedCount 的一些关键功能,希望对知识有所帮助。

underestimatedCount 承诺集合的数量不会大于序列中元素的数量,并且在 序列协议 中默认值为零。 Check

@inlinable
  public var underestimatedCount: Int {
    return 0
  }

此外,在收集协议中,其默认值与计数相同。 Check

@inlinable
  public var underestimatedCount: Int {
    // TODO: swift-3-indexing-model - review the following
    return count
  }

因此,由于这种默认实现,直接使用 Collection's underestimatedCount 肯定不如使用 Sequence's 常见,因为 Collection 保证了非破坏性迭代,并且在大多数情况下,underestimatedCount 只会返回 count

当然,自定义集合类型可以提供它们自己的 underestimatedCount 实现——给出它们包含多少元素的下限,以一种可能比它们的 count 实现更有效的方式,这可能很有用.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 2018-02-20
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2018-08-03
    相关资源
    最近更新 更多