That's my Question =)
MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];
How can I get the number of albums of each artists of the MPMediaItemCollections in songsByArtist ?
Exemple :
The Beatles 3 Albums
AC/DC 6 Albums
Thank You !!
The
artistsQuery
convenience constructor does not sort and group by album.artistsQuery
returns an array of media item collections of all artists sorted alphabetically by artist name. Nested inside each artist collection is an array of media items associated with all songs for that artist. The nested array is sorted alphabetically by song title.One way to keep a count of albums by artist is to enumerate through all the song items for each artist collection and use a
NSMutableSet
to keep track of distinct album titles associated with each song. Then add the count of the set as the value for each artist key in aNSMutableDictionary
. Any duplicate album titles will not be added since aNSMutableSet
will only take distinct objects:It would be cleaner if you change the query to
albumsQuery
. This query groups and sorts the collection by album name. Then it is just a matter of enumerating through the array of album collections and keeping a count of the representative artist name for each album in aNSCountedSet
. The counted set will track the number of times objects are inserted:You can also retrieve the count for a given object in a
NSCountedSet
with thecountForObject:
method.