ARQuicklook - Fatal error: Unexpectedly found nil while unwrapping an Optional value

ARQuicklook - Fatal error: Unexpectedly found nil while unwrapping an Optional value

我正在关注 this tutorial 如何制作 AR Quicklook 应用。只有几个步骤,看起来很简单。但是在最后一步我得到了一个致命错误,因为 let 变量被强制展开。我尝试将其设为可选,但出现不同的错误,例如:

Optional chain has no effect, expression already produces 'URL?'

如果我删除可选的,我会在下一行收到此警告:

'URL?' is not convertible to 'QLPreviewItem'; did you mean to use 'as!' to force downcast?

如果我强行解开这一行,应用程序就会崩溃。我不知道如何解决这个问题。我什至看过官方视频 here 并且在大约 14:30 分钟时他们也有相同的代码,他们强制展开那条线。

@IBOutlet var collectionView: UICollectionView!
let models = ["A", "B", "C", "D", "E"]

var thumbnails = [UIImage]()
var thumbnailIndex = 0

override func viewDidLoad() {
    super.viewDidLoad()
    for model in models {
        if let thumbnail = UIImage(named: "\(model).jpg") {
            thumbnails.append(thumbnail)
        }
    }

    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.reloadData()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return models.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LibraryCell", for: indexPath) as? LibraryCollectionViewCell

    if let cell = cell {
        cell.modelThumbnail.image = thumbnails[indexPath.item]
        let title = models[indexPath.item]
        cell.modelTitle.text = title.capitalized
    }

    return cell!
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    thumbnailIndex = indexPath.item

    let previewController = QLPreviewController()
    previewController.dataSource = self
    previewController.delegate = self
    present(previewController, animated: true)
}

func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
    return 1
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    let url = Bundle.main.url(forResource: models[thumbnailIndex], withExtension: "usdz")!
    return url as QLPreviewItem
}

这个

let url = Bundle.main.url(forResource: models[thumbnailIndex], withExtension: "usdz")!

如果主捆绑包中不存在该项目,或者存在但未检查目标成员资格,则只能return零,因此请验证所有这些资源是否存在

A.usdz,B.usdz,C.usdz,D.usdz,E.usdz

教程制作者在此处显示