我是跟踪本教程如何制作一个AR快速外观应用程序。只有几个步骤看起来很简单。然而,在最后一步中,我得到了一个致命的错误,因为let变量正被强制展开。我试着把它变成一个可选的,但是我得到了不同的错误,比如:
可选链没有作用,表达式已经生成'URL?‘
如果删除可选选项,则在下一行中得到以下警告:
“‘URL?”不可转换为“QLPreviewItem”;您是否有意使用“as!”强迫他们下台吗?
如果我强行拆开这一行,应用程序就会崩溃。我想不出该怎么绕开这件事。我甚至看过官方视频这里,在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
}发布于 2018-09-16 20:20:04
这
let url = Bundle.main.url(forResource: models[thumbnailIndex], withExtension: "usdz")!只有在主包中不存在项,或者存在但没有检查目标成员资格时,才能返回零,因此请验证所有这些资源是否存在。
A.usdz,B.usdz,C.usdz,D.usdz,E.usdz
教程制作者在这里展示了

https://stackoverflow.com/questions/52357892
复制相似问题