###前提・実現したいこと
MPMediaQuery.artists()
のcollectionSections
配列から、セクションのタイトルを取り出したいので、以下のメソッドで取り出そうとしています。
swift
1 sectionData = getSectionInfo() 2 //省略 3 func getSectionInfo() -> Array<MPMediaQuerySection> { 4 var sectionArray = Array<MPMediaQuerySection>() 5 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 6 let artistSection = artistQuery.collectionSections! 7 sectionArray = artistSection 8 return sectionArray 9 } 10 //省略 11 label.text = self.sectionData[section].title
###発生している問題・エラーメッセージ
しかし、上記のsectionData[section].title
の部分に、
Cannot subscript a value of type 'Array<MPMediaQuerySection>' with an index of type '(section).Type' (aka 'section.type')
というエラーが出てしまいます。
###試したこと
以前、同じようにcollectionSections
から値を取り出して、UITableViewのセクションタイトルに代入するメソッドでは上記コードで通ったため、正直何がなにやら、というところです・・・
swift
1func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 2 return self.sectionData[section].title 3} 4//これはOKでした。
titleForHeaderInSection
メソッドにしても、label.text
にしても、String型を返せばいいわけですから、何が違うのだろうと思ってしまうのです。
また、やはりテーブルビューでは有効だったので、collectionSections
が取り出せない、理解していないという可能性もないでしょうし・・・
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
マルチポストはおやめください。https://ja.stackoverflow.com/questions/40446/