###前提・実現したいこと
テーブルビューにデバイス上にある曲のアーティストを並べて表示したいのです。
そこで、以下のようにコーディングしました。
swift
1import UIKit 2import MediaPlayer 3 4class ArtistTabView: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 @IBOutlet var artistTableView: UITableView! 7 8 var artistSectionData = Array<MPMediaQuerySection>() 9 var artistCollectionData = Array<MPMediaItemCollection>() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 // Do any additional setup after loading the view, typically from a nib. 14 artistTableView.delegate = self 15 artistTableView.dataSource = self 16 artistSectionData = getSectionInfo() 17 artistData = getArtistInfo() 18 artistTableView.reloadData() 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 // Dispose of any resources that can be recreated. 24 } 25 26 27 func getSectionInfo() -> Array<MPMediaQuerySection> { 28 var array = Array<MPMediaQuerySection>() 29 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 30 let artistSection = artistQuery.collectionSections! 31 array = artistSection 32 return array 33 } 34 35 func getArtistInfo() -> Array<MPMediaItemCollection> { 36 var artistArray = Array<MPMediaItemCollection>() 37 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 38 let artistCollection = artistQuery.collections! 39 artistArray = artistCollection 40 return artistArray 41 } 42 43 44 func numberOfSections(in tableView: UITableView) -> Int { 45 return self.artistSectionData.count 46 } 47 48 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 49 return self.artistSectionData[section].range.length 50 } 51 52 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 53 return self.artistSectionData[section].title 54 } 55 56 func sectionIndexTitles(for tableView: UITableView) -> [String]? { 57 return artistSectionData.map { $0.title } 58 } 59 60 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 61 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! 62 let section = self.artistSectionData[indexPath.section] 63 let collection = self.artistCollectionData[section.range.location + indexPath.row] 64 cell.textLabel?.text = collection.representativeItem?.artist 65 return cell 66 } 67}
ちなみに、ローンチ画面のボタンを押してタブビューを呼び出し、そこにテーブルビューを置いています。
###発生している問題・エラーメッセージ
上記コードでビルドタイムエラーはでないのですが、
ローンチ画面のボタンを押して上記の「テーブルビューを置いたタブビュー」を表示するタイミングで、コンソールエリアに以下のエラーが出てアプリが落ちます。
unexpectedly found nil while unwrapping an Optional value
デバッグナビゲーターは、
0x100358448 <+120>: brk #0x1 <Thread 1: EXC_BREAKPOINT (code=1, subside-0x100358448)
となっています。
一番最後の行です。
###該当のソースコード
swift
1func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! 3 let section = self.artistSectionData[indexPath.section] 4 let collection = self.artistCollectionData[section.range.location + indexPath.row] 5 cell.textLabel?.text = collection.representativeItem?.artist 6 return cell 7 }
のlet collection
の値がnil
になっているのかなと思っていますが、
実際にはよく、わかりません・・・
###試したこと
print()
などで監視してみようとしても、表示した瞬間にはアプリが落ちてしまい、コンソールエリアにはunexpectedly found nil while unwrapping an Optional value
がでてしまって・・・
###補足情報(言語/FW/ツール等のバージョンなど)
Swift3 xcode8
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/17 11:32