【问题标题】:How to pull the artist value from MPMediaItemCollection如何从 MPMediaItemCollection 中提取艺术家值
【发布时间】:2016-07-23 07:39:59
【问题描述】:

为什么下面的结果是一个充满“艺术家”的表格视图,而不是一个充满实际艺术家姓名的表格视图?我哪里做错了?如何从收藏中提取艺术家价值?感谢所有帮助...

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
}

【问题讨论】:

    标签: swift media-queries tableview media-player mpmediaitemcollection


    【解决方案1】:

    您可以通过representativeItem 访问MPMediaItemCollection 的属性。您可以这样获取艺术家的姓名:

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

    如果我是你,我不会强行打开 tableData.collections,因为如果你的用户有一个空的 iTunes 库,那一行会导致应用程序崩溃。

    【讨论】:

    • xcode 强迫我做类似 (tableData.collections?) 之类的事情!这个好点了吗?
    • @rocketman240 您想使用可选绑定或检查guard 以确保tableData.collections 不为零。例如if let collections = tableData.collections { // proceed as you did before }guard let collections = tableData.collections else { // handle else }
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多