【问题标题】:search for iPod artists starting with a letter搜索以字母开头的 iPod 艺术家
【发布时间】:2013-04-28 22:11:42
【问题描述】:
有没有办法使用通配符之类的东西来搜索以给定字母开头的所有 iPod 艺术家姓名,如下所示:
MPMediaPropertyPredicate *artistNamePredicate = [MPMediaPropertyPredicate predicateWithValue:@"A*" forProperty:MPMediaItemPropertyArtist];
MPMediaQuery *allArtistsQuery = [MPMediaQuery artistsQuery];
[allArtistsQuery addFilterPredicate: artistNamePredicate];
【问题讨论】:
标签:
iphone
ios
mpmediaquery
【解决方案1】:
MPMediaPropertyPredicate 仅支持属性值等于谓词值(默认)或包含谓词值,如docs 中所述。
话虽如此,另一种方法是使用 contains 比较,然后使用返回值过滤结果。
[MPMediaPropertyPredicate predicateWithValue:@"A*" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonContains]
【解决方案2】:
您可以使用MPMediaQuery 上的collectionSections 属性来获取数据的相关部分。对于artistsQuery,每个MPMediaQuerySection 的title 代表艺术家姓名的第一个字母。每个部分还有一个range,然后您可以应用它从collections 数组中获取艺术家姓名的子数组。
这将为您提供字母 A 的 MPMediaQuerySection:
MPMediaQuery *allArtistsQuery = [MPMediaQuery artistsQuery];
NSArray *collectionSections = allArtistsQuery.collectionSections;
NSPredicate *artistPredicate = [NSPredicate predicateWithFormat:@"title == %@", @"A"];
MPMediaQuerySection *artistSection = [[collectionSections filteredArrayUsingPredicate:artistPredicate] lastObject];
然后获取该部分的range 属性以获取以字母A开头的所有艺术家收藏的子数组:
NSArray *collections = allArtistsQuery.collections;
NSRange arraySlice = artistSection.range;
NSArray *filteredCollections = [collections subarrayWithRange:arraySlice];
for (MPMediaItemCollection *artistCollection in filteredCollections) {
NSLog(@"%@", [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist]);
}