此问题在此处已有答案:
What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?(18回答)
上个月关门了。
我正在创建集合视图与屏幕过渡,然后一个错误,这是“线程1:致命错误:在隐式解 Package Optional值”时意外发现nil。我附上了错误的图片。Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value的
我不知道该如何修复这个错误。下面是我的编程。
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet var Mylabel :UILabel!
@IBOutlet var Myimage :UIImageView!
func setup(with hero: Hero) {
Myimage.image = hero.image
Mylabel.text = hero.name
}
}
import UIKit
struct Hero {
let name: String
let image: UIImage
}
let heros: [Hero] = [
Hero(name: "1", image: #imageLiteral(resourceName: "1.png")),
Hero(name: "2", image: #imageLiteral(resourceName: "2.png")),
Hero(name: "3", image: #imageLiteral(resourceName: "3.png")),
Hero(name: "4", image: #imageLiteral(resourceName: "4.png")),
Hero(name: "5", image: #imageLiteral(resourceName: "5.png"))
]
class ViewController: UIViewController {
// var herosList = [Heros]()
@IBOutlet weak var MyCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
MyCollectionView.dataSource = self
MyCollectionView.delegate = self
MyCollectionView.collectionViewLayout = UICollectionViewFlowLayout()
}
}
extension ViewController: UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return heros.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath) as! MyCollectionViewCell
cell.setup(with: heros[indexPath.row])
return cell
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 82, height: 120)
}
}
extension ViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let detailViewController = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController else {
fatalError("Failed to instantiate DetailViewController from storyboard.")
}
detailViewController.sproduct = heros[indexPath.row]
navigationController?.pushViewController(detailViewController, animated: true)
}
我谷歌了一下。但我不知道
1条答案
按热度按时间rkkpypqq1#
这可能是因为你没有这个值,你正在强制展开它,请尽量不要强制展开,直到你确定。