質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

446閲覧

Swift UICollectionViewControllerにてエラー「Thread 1: Fatal error: Unexpectedly found nil while unwrappin」

ksnv35kj

総合スコア10

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/05/24 14:17

前提・実現したいこと

iOSアプリを初めて作っているのですがエラーが発生して作業が止まってしまっています。。

発生している問題・エラーメッセージ

Thread 1: Fatal error: Unexpectedly found nil while unwrappin

エラーが出ているのは下記ソースの「ここでエラーが発生」の部分です

該当のソースコード

swift

1import UIKit 2 3private let reuseIdentifier = "Cell" 4 5class BooksCollectionViewController: UICollectionViewController { 6 7 let movieData: [[String]] = [["タイトル1", "movie1.mp4","image1"],["タイトル2", "movie1.mp4","image2"]] 8 9 override func viewDidLoad() { 10 11 // background image 12 let bg = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)) 13 bg.image = UIImage(named: "sky") 14 bg.layer.zPosition = -1 15 self.view.addSubview(bg) 16 17 super.viewDidLoad() 18 19 // Uncomment the following line to preserve selection between presentations 20 // self.clearsSelectionOnViewWillAppear = false 21 22 // Register cell classes 23 self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 24 25 // Do any additional setup after loading the view. 26 } 27 28 /* 29 // MARK: - Navigation 30 31 // In a storyboard-based application, you will often want to do a little preparation before navigation 32 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 33 // Get the new view controller using [segue destinationViewController]. 34 // Pass the selected object to the new view controller. 35 } 36 */ 37 38 // MARK: UICollectionViewDataSource 39 40 override func numberOfSections(in collectionView: UICollectionView) -> Int { 41 // #warning Incomplete implementation, return the number of sections 42 return 1 43 } 44 45 46 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 47 // #warning Incomplete implementation, return the number of items 48 return self.movieData.count 49 } 50 51 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 52 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) 53 54 // Configure the cell 55 let imageView = cell.contentView.viewWithTag(1) as! UIImageView //「ここでエラーが発生」 56 57 // 画像配列の番号で指定された要素の名前の画像をUIImageとする 58 let cellImage = UIImage(named: self.movieData[indexPath.row][2]) 59 60 // UIImageをUIImageViewのimageとして設定 61 imageView.image = cellImage 62 63 // Configure the cell 64 65 return cell 66 } 67 68} 69

試したこと

UIImageにはTagに1を設定しています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

viewWithTag には以下のようにUIViewのオプショナル型が返ってくることが示されています。

Swift

1func viewWithTag(_ tag: Int) -> UIView?

viewWithTagの結果がnilだったため、UIImageViewにキャストしようとしてもエラーとなっているようです。

投稿2019/05/24 14:43

nakasho_dev

総合スコア2655

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ksnv35kj

2019/05/24 15:07

わぁ本当だ。。 中身見たらnilでした… しかしどうすれば…状態です(情けない)
ksnv35kj

2019/05/24 15:15

CellのIDを「Cell」から「Cell1」に変更したところ解決しました! ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問