如何从MPMediaItemCollection中获取艺术家的价值 [英] How to pull the artist value from MPMediaItemCollection

查看:115
本文介绍了如何从MPMediaItemCollection中获取艺术家的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下结果导致充满艺术家"的表格视图而不是充满实际艺术家名称的表格视图?我哪里做错了?如何从收藏中汲取艺术家的价值?感谢所有帮助...

Why does the following result in a tableview full of "Artist" instead of a tableview full of actual artist names? Where did I go wrong? How do I pull the artist value from a collection? All help is appreciated...

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
}

推荐答案

您可以通过representativeItem来访问MPMediaItemCollection的属性.您可以像这样提取艺术家的姓名:

You access the properties of a MPMediaItemCollection via it's representativeItem. You fetch the artist's name like so:

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

尽管如果您是我,也不会强行打开tableData.collections,因为如果您的用户的iTunes库为空,则该行将导致应用程序崩溃.

Though if I were you I wouldn't forcibly unwrap tableData.collections because if your user has an empty iTunes library that line will case the application to crash.

这篇关于如何从MPMediaItemCollection中获取艺术家的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆