【问题标题】:iOS - NSInternalInconsistencyException happens on iOS 9 and 10 but working fine on iOS 11iOS - NSInternalInconsistencyException 在 iOS 9 和 10 上发生,但在 iOS 11 上运行良好
【发布时间】:2017-12-07 15:39:40
【问题描述】:

这是我在使用 iOS 9 和 10 的设备上遇到的错误:

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“请求数量 第 9223372036854775807 节之前的项目只有 1 集合视图中的部分'

这个错误对我来说似乎很清楚,但是我无法理解为什么在装有 iOS 11 的设备上没有发生这种情况。

我不知道如何解决它。

这是我的代码:

extension MainTileViewController: MainForecastsDelegate {
func mainForecasts(_ forecastVC: MainForecastsViewController!, didChangeWith object: Any!) {
    if let cell = self.outletWeatherForecastCollectionView.cellForItem(at: self.currentIndexPath) as? MainTileCollectionViewCell { 
        // Some stuff...
    }
}

崩溃发生在这里。这是用户切换天等时触发的协议方法...

显然我的 currentIndexPath 有问题。

这是我的初始化:

 var currentIndexPath : IndexPath = []

在 viewDidLoad 中:

self.currentIndexPath = IndexPath(item: 0, section: 0)

如何保护我的代码,使其不会崩溃?你能解释一下从 iOS 9/10 到 iOS 11 的 collectionView 之间的变化行为吗(预取除外)。

【问题讨论】:

    标签: ios swift uicollectionview indexpath


    【解决方案1】:

    发生的事情是设置currentIndexPath = [] 没有为其itemsection 分配任何值;它正在创建一个“空” IndexPath (IndexPath 对象基本上是任意长度的元组或值数组,可以这样创建/操作)。任何试图以这种方式使用它的代码(例如将其传递给cellForItem 调用)都可能具有未定义的行为。看起来有些东西有效地将缺少的 section 值解释为 -1,然后其他东西将 -1 解释为 unsigned 64-bit integer

    相反,如果您想使用与现在相同的一般逻辑,我建议将indexPath 声明为可选:

    var currentIndexPath: IndexPath?
    

    然后,在 currentIndexPath 的使用中使用 if-let 语法:

    extension MainTileViewController: MainForecastsDelegate {
    func mainForecasts(_ forecastVC: MainForecastsViewController!, didChangeWith object: Any!) {
        if let currentIndexPath = currentIndexPath,
            let cell = outletWeatherForecastCollectionView.cellForItem(at: currentIndexPath) as? MainTileCollectionViewCell { 
            // Some stuff...
        }
    }
    

    这遵循 Swift 习惯用法,并明确了以“未知”索引路径开头的概念。

    但是 - 作为@picciano's answer suggests,您可能需要重新考虑您的整体设计,以更好地适应 iOS 应用程序的更大设计模式。

    【讨论】:

      【解决方案2】:

      我建议稍微改变一下你的方法。让您的集合视图单元子类负责更新自身,可能来自通知。尝试保留索引路径或对单元格的引用总是有问题的,因为它们倾向于被重用。

      【讨论】:

        猜你喜欢
        • 2017-03-12
        • 2017-02-04
        • 1970-01-01
        • 2018-08-01
        • 2012-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        相关资源
        最近更新 更多