Swift打印个人媒体库中的流派

时间:2018-02-08 21:13:49

标签: ios swift apple-music

我正在尝试在我的个人图书馆中打印出一系列类型。我遇到了一个很大的问题,我无法得到这些类型的实际名称。

我的代码是:

// Initialization: 
let query = MPMediaQuery()

let result = query.items
print("count:  \(result?.count)") //This returns the amount of items in my library

现在问题在于:

query.groupingType = MPMediaGrouping.genre 
var genreArray = query.collections
print(genreArray)

当我打印出来时,它会给我一个带有十六进制跟踪的MPConcreteMediaItemCollection项目列表:

<MPConcreteMediaItemCollection: 0x1c00cbf30>, <MPConcreteMediaItemCollection: 0x1c00cbfa0>, <MPConcreteMediaItemCollection: 0x1c00cc010>, <MPConcreteMediaItemCollection: 0x1c00cc080>, <MPConcreteMediaItemCollection: 0x1c00cc0f0>, <MPConcreteMediaItemCollection: 0x1c00cc160>, <MPConcreteMediaItemCollection: 0x1c00cc1d0>, <MPConcreteMediaItemCollection: 0x1c00cc240>, <MPConcreteMediaItemCollection: 0x1c00cc2b0>, <MPConcreteMediaItemCollection: 0x1c00cc320>, <MPConcreteMediaItemCollection: 0x1c00cc390>, <MPConcreteMediaItemCollection: 0x1c00cc400>, <MPConcreteMediaItemCollection: 0x1c00cc470>, <MPConcreteMediaItemCollection: 0x1c00cc4e0>, <MPConcreteMediaItemCollection: 0x1c00cc550>, <MPConcreteMediaItemCollection: 0x1c00cc5c0>, <MPConcreteMediaItemCollection: 0x1c00cc630>, <MPConcreteMediaItemCollection: 0x1c00cc6a0>, <MPConcreteMediaItemCollection: 0x1c00cc710>, <MPConcreteMediaItemCollection: 0x1c00cc780>, <MPConcreteMediaItemCollection: 0x1c00cc7f0>, <MPConcreteMediaItemCollection: 0x1c00cc860>, <MPConcreteMediaItemCollection: 0x1c00cc8d0>, <MPConcreteMediaItemCollection: 0x1c00cc940>, <MPConcreteMediaItemCollection: 0x1c00cc9b0>, <MPConcreteMediaItemCollection: 0x1c00cca20>, <MPConcreteMediaItemCollection: 0x1c00cca90>, <MPConcreteMediaItemCollection: 0x1c00ccb00>, <MPConcreteMediaItemCollection: 0x1c00ccb70>, <MPConcreteMediaItemCollection: 0x1c00ccbe0>, <MPConcreteMediaItemCollection: 0x1c00ccc50>, <MPConcreteMediaItemCollection: 0x1c00cccc0>, <MPConcreteMediaItemCollection: 0x1c00ccd30>, <MPConcreteMediaItemCollection: 0x1c00ccda0>, <MPConcreteMediaItemCollection: 0x1c00cce10>, <MPConcreteMediaItemCollection: 0x1c00cce80>, <MPConcreteMediaItemCollection: 0x1c00ccef0>, <MPConcreteMediaItemCollection: 0x1c00ccf60>, <MPConcreteMediaItemCollection: 0x1c00ccfd0>, <MPConcreteMediaItemCollection: 0x1c00cd040>, <MPConcreteMediaItemCollection: 0x1c00cd0b0>])

如何让这些值等同于实际的流派名称?

1 个答案:

答案 0 :(得分:1)

HereMPMediaItemCollection的文档,它是query.collections中存储的对象。

每个MPMediaItemCollection都有items属性,这是MPMediaItem s的数组。每个MPMedaItem都有genre属性,这正是您要查找的内容。

如果您的查询的谓词是按类型,那么您可以说:

for collection in query.collections {
    let genre = collection.representativeItem?.genre
    // ...
}

否则,您将不得不遍历每个项目:

for collection in query.collections {
    for item in collection.items {
        let genre = item.genre
        // ...
    }
}