首页 > 解决方案 > 从 makeItem 崩溃中向下转换 NSCollectionViewItem

问题描述

我有一个自定义项目类

class AttachmentCell: NSCollectionViewItem {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }

}

我尝试在委托中实例化为

    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {

        let item = collectionView
            .makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AttachmentCell"),
                      for: indexPath) as! AttachmentCell

        return item
    }

现在程序因错误而崩溃

Could not cast value of type 'NSCollectionViewItem' (0x7fffa5050690) to 'Notes.AttachmentCell' (0x10000ee38).
2019-06-04 13:01:33.603441+0200 Notes[18477:37049533] Could not cast value of type 'NSCollectionViewItem' (0x7fffa5050690) to 'Notes.AttachmentCell' (0x10000ee38).

我的理解是 makeItem 应该给我一个我给它的类型的对象(我想我以前看过这个工作),所以我不知道这里发生了什么。

标签: arraysxcodemacos

解决方案


好的,答案很简单,我在 nib 文件中放错了类:-S


推荐阅读