ios - Swift: "Fatal error: newElements.underestimatedCount was an overestimate"- 这个错误是什么意思?

标签 ios swift uitableview fatal-error

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

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

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

有时,在运行应用程序时我会收到此错误: 线程 1: fatal error :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

有人可以解释为什么会发生这种情况吗?我真的很感激任何帮助!

数据源方法(来自 View Controller ):

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)
    }

这是我的项目:

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
    }
}

最佳答案

你的代码绝对没问题,很难说出错误的根本原因。以下是有关 underestimatedCount 的一些主要功能希望它对知识目的有所帮助。

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

@inlinable
  public var underestimatedCount: Int {
    return 0
  }

此外,在收集协议(protocol)中,其默认值与count相同。 Check

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

因此,由于此默认实现,请使用集合 underestimatedCount直接肯定不如使用序列常见,因为 Collection 保证非破坏性迭代,并且在大多数情况下 underestimatedCount只会返回 count .

当然,自定义集合类型可以提供自己的 underestimatedCount 实现– 以可能比 count 更有效的方式给出它们包含的元素数量的下限实现,这可能会有用。

关于ios - Swift: "Fatal error: newElements.underestimatedCount was an overestimate"- 这个错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650406/

相关文章:

swift - 警告 : Attempt to present <UIImagePickerController: > on <. ..> 已经呈现(空)

ios - iOS 上的 Realm : crash while creating nested object with primaryKey

swift - TableView header 内的编程 UIView

ios - 将数据源设置为 UITableView 时出现运行时错误

ios - 将 lame mp3 编码器链接到 iOS 应用程序时如何解决 "Undefined symbol init_xrpow_core_sse"?

ios - 从 iOS 应用程序通过 node.js 将图像上传到服务器

iOS:文本字段委托(delegate),如何在 TableView 中处理行移动之前自动结束编辑?

ios - 使用 Twitter 反向身份验证在 Rails 后端创建用户

ios - 组合框架更新 UI 无法正常工作

ios - 在 UICollectionViewCell 中使用 UITableView(模仿 Google Places 底部滚动列表)