Question

Is there a way to use something like a wildcard character to search for all iPod artist names starting with a given letter, something like this:

MPMediaPropertyPredicate *artistNamePredicate = [MPMediaPropertyPredicate predicateWithValue:@"A*" forProperty:MPMediaItemPropertyArtist];

MPMediaQuery *allArtistsQuery = [MPMediaQuery artistsQuery];

[allArtistsQuery addFilterPredicate: artistNamePredicate];
Was it helpful?

Solution

MPMediaPropertyPredicate only supports the property value being equal to the predicate value (the default) or containing the predicate value, as noted in the docs.

That being said, an alternative is to use the contains comparison and then filter the results with the returned values.

[MPMediaPropertyPredicate predicateWithValue:@"A*" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonContains]

OTHER TIPS

You could use the collectionSections property on MPMediaQuery to get the relevant section of your data. For the artistsQuery, the title of each MPMediaQuerySection represents the first letter of an artist name. Each section also has a range, which you could then apply to get a subarray of artist names from the collections array.

This will give you the MPMediaQuerySection for the letter A:

MPMediaQuery *allArtistsQuery = [MPMediaQuery artistsQuery];
NSArray *collectionSections = allArtistsQuery.collectionSections;
NSPredicate *artistPredicate = [NSPredicate predicateWithFormat:@"title == %@", @"A"];

MPMediaQuerySection *artistSection = [[collectionSections filteredArrayUsingPredicate:artistPredicate] lastObject];

Then take the range property of that section to get a subarray of all artist collections starting with the letter A:

NSArray *collections = allArtistsQuery.collections;
NSRange arraySlice = artistSection.range;
NSArray *filteredCollections = [collections subarrayWithRange:arraySlice];

for (MPMediaItemCollection *artistCollection in filteredCollections) {
    NSLog(@"%@", [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top