Swift: "Fatal error: newElements.underestimatedCount was an overestimate" - what does this error mean?
Swift: "Fatal error: newElements.underestimatedCount was an overestimate" - what does this error mean?
我有一个 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
}
}
您的代码绝对没问题,很难说出错误的根本原因。以下是有关 underestimatedCount
的一些主要功能,希望对知识有所帮助。
underestimatedCount
承诺集合的计数不会大于序列中元素的数量,并且在 序列协议中 默认值为零。 Check
@inlinable
public var underestimatedCount: Int {
return 0
}
此外,在collection protocol中,它的默认值与count相同。 Check
@inlinable
public var underestimatedCount: Int {
// TODO: swift-3-indexing-model - review the following
return count
}
因此,由于此默认实现,直接使用 Collection 的 underestimatedCount
肯定不如使用 Sequence 的 常见,因为 Collection 保证了非破坏性迭代,并且在大多数情况下 underestimatedCount
只会 return count
.
当然,自定义集合类型可以提供它们自己的 underestimatedCount
实现——以一种可能比它们的 count
实现更有效的方式给出它们包含的元素数量的下限,这可能会有用。
我有一个 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
}
}
您的代码绝对没问题,很难说出错误的根本原因。以下是有关 underestimatedCount
的一些主要功能,希望对知识有所帮助。
underestimatedCount
承诺集合的计数不会大于序列中元素的数量,并且在 序列协议中 默认值为零。 Check
@inlinable
public var underestimatedCount: Int {
return 0
}
此外,在collection protocol中,它的默认值与count相同。 Check
@inlinable
public var underestimatedCount: Int {
// TODO: swift-3-indexing-model - review the following
return count
}
因此,由于此默认实现,直接使用 Collection 的 underestimatedCount
肯定不如使用 Sequence 的 常见,因为 Collection 保证了非破坏性迭代,并且在大多数情况下 underestimatedCount
只会 return count
.
当然,自定义集合类型可以提供它们自己的 underestimatedCount
实现——以一种可能比它们的 count
实现更有效的方式给出它们包含的元素数量的下限,这可能会有用。