Question

The following code, is used to partition and add sections to a list of songs.

    query = [MPMediaQuery songsQuery];
    [query addFilterPredicate: artistNamePredicate];            
    NSArray *itemsFromArtistQuery = [query items];  
    self.artist1 = [self partitionObjects:itemsFromArtistQuery collationStringSelector:@selector(title)];

Works perfectly. However when I try to do it with:

    query = [MPMediaQuery albumsQuery]; //same with playlistsQuery, artistsQuery, genresQuery
    [query addFilterPredicate: artistNamePredicate];            
    NSArray *itemsFromArtistQuery = [query collections];    
    self.artist1 = [self partitionObjects:itemsFromArtistQuery collationStringSelector:@selector(title)];

I get a SIGABRT error every single time. I've attributed it to the "collections" part of the code, as that's the only difference in the whole block. I've tried changing "title" to "name" "albumTitle", "playlist", "genre" and more, but I still end up with:

"-[MPConcreteMediaItemCollection title]: unrecognized selector sent to instance"

Can anybody help me here? I'm ready to rip my hair out!

THANK-YOU!

BenBen

Was it helpful?

Solution

You are right that the problem is with the collections part. The collationStringSelector: must be a method that returns a NSString for the objects your passing it, in this case MPMediaItemCollection's.

(It worked in the first case because you were passing MPMediaItem's which do respond to title).

Here we select each MPMediaItemCollection from the artistCollections array and then get a single MPMediaItem that represents the whole collection. We can then get the artists name and add it to an array.

query = [MPMediaQuery albumsQuery]; //same with playlistsQuery, artistsQuery, genresQuery
[query addFilterPredicate: artistNamePredicate];            
NSArray *artistCollections = [query collections];
NSMutableArray *artists = [NSMutableArray array];

for (MPMediaItemCollection *artist in artistCollections) {
    // get a single MPMediaItem that represents the collection
    MPMediaItem *representativeItem = [artist representativeItem];
    NSString *artistName = [representativeItem valueForProperty:MPMediaItemPropertyArtist];
    [artists addObject:artistName];
}

self.artist1 = [self partitionObjects:artists collationStringSelector:@selector(self)];

Now we are passing an array of NSString's so we set the collationStringSelector: to self which will return the artists name as a NSString.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top