我创建了一个数组,它是我的集合视图的数据。我正在尝试点击 CollectionViewCell 并播放包含在我的数组的文件组件中的声音。我不知道如何播放声音,甚至无法开始,因为我的 xcode 项目中的文件为空值。
错误:线程 1:致命错误:在展开可选值时意外发现 nil
如果我不强制打开文件,它会给我一个错误......
class ViewController: UIViewController {
let sounds : [Sounds] = [Sounds(statement: "A", file: Bundle.main.url(forResource: "A", withExtension: "aifc")!),
Sounds(statement: "B", file: Bundle.main.url(forResource: "B", withExtension: "aifc")!),
Sounds(statement: "C", file: Bundle.main.url(forResource: "C", withExtension: "aifc")!),
Sounds(statement: "D", file: Bundle.main.url(forResource: "D", withExtension: "aifc")!)]
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sounds.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "soundCell", for: indexPath) as! CollectionViewCell
let Soundz = sounds[indexPath.item]
cell.cellLabel.text = Soundz.statement
return cell
}
}
struct Sounds{
var statement : String
var file : URL
}