Instance Property

collections

An array of media item collections whose contained items match the query’s media property predicate.

Declaration

var collections: [MPMediaItemCollection]? { get }

Discussion

The returned array of collections are grouped and sorted by the groupingType of the media query. The following code snippet illustrates how to use this property:

// Specify a media query; this one matches the entire library because it
// does not contain a media property predicate
let everything = MPMediaQuery()
 
// Configure the media query to group its media items; here, grouped by artist
everything.groupingType = MPMediaGrouping.album
 
// Obtain the media item collections from the query
let collections = [everything]

Each element of the collections array now contains a media item collection. Each collection contains the media items from the library by a particular artist. The elements of the array are sorted by artist name.

For the available grouping types, see MPMediaGrouping.

See Also

Performing Media Queries

var items: [MPMediaItem]?

An array of media items that match the media query’s predicate.