0

なぜ実際のアーティスト名のテーブルビューではなく、「アーティスト」でいっぱいのテーブルビューに次の結果が出るのですか?どこで私は間違えましたか?アーティストの価値をコレクションから引き出すにはどうすればよいですか?すべてのヘルプは...MPMediaItemCollectionからアーティスト値を引き出す方法

var tableData = MPMediaQuery.artistsQuery() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.artistTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    tableData.groupingType = MPMediaGrouping.Artist 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.tableData.collections!.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell  { 
    let cell: UITableViewCell = self.artistTableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell 
    let artist: MPMediaItemCollection = tableData.collections![indexPath.row] 

    if artist.valueForProperty(MPMediaItemPropertyArtist) == nil { 
     cell.textLabel?.text = "Artist" as String 
    } else { 
    let artistName = artist.valueForProperty(MPMediaItemPropertyArtist) as! NSString 
    cell.textLabel?.text = artistName as String 
    } 
    return cell 
} 

答えて

1

を高く評価され、それはrepresentativeItemだ経由であなたはMPMediaItemCollectionのプロパティにアクセスします。あなたはそうのようなアーティストの名前をフェッチ:

let artist : MPMediaItemCollection = tableData.collections![indexPath.row] 
let artistName = artist.representativeItem.artist ?? "Unknown Artist" 

私があなただったら、ユーザーはラインがクラッシュするアプリケーションを小文字になる空のiTunesライブラリを持っている場合、私は強制的理由tableData.collectionsをアンラップないだろうけど。

+0

これはうまくいきました...ありがとう! – rocketman240

+0

xcodeは私に(tableData.collections?)のようなことを強いる!これはもっと良いですか? – rocketman240

+0

@ rocketman240あなたはオプションのバインディングを使用するか、 'guard'をチェックして' tableData.collections'がnilでないことを確認します。例えば ​​'let let collections = tableData.collections {//前と同じように処理する}'または 'guard let collections = tableData.collections else {// else else}' – Wes

関連する問題