I am following this tutorial on how to make an AR Quicklook app. There are only a few steps which seems simple enough. However on the last step I am getting a fatal error because let variable is being force unwrapped. I have tried making it an optional instead but I get different errors like:
Optional chain has no effect, expression already produces 'URL?'
If I remove the optional I then get this warning on the next line down:
'URL?' is not convertible to 'QLPreviewItem'; did you mean to use 'as!' to force downcast?
If I force unwrap this line the app crashes. I can't work out how to get around this. I have even looked at the official video here and at around 14:30 minutes they also have the same code where they are force unwrapping that line.
@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
}
This
let url = Bundle.main.url(forResource: models[thumbnailIndex], withExtension: "usdz")!
can only return nil if the item doesn't exist in main Bundle , or exists but target membership isn't checked , so verify that all these resources exist
A.usdz,B.usdz,C.usdz,D.usdz,E.usdz
the tutorial maker shows that here