2013-04-28 117 views
0

iPod的艺术家有没有使用像通配符搜索与给定的字母开始,像这样所有的iPod艺术家姓名的方式:搜索以字母开头

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

MPMediaQuery *allArtistsQuery = [MPMediaQuery artistsQuery]; 

[allArtistsQuery addFilterPredicate: artistNamePredicate]; 

回答

0

MPMediaPropertyPredicate只支持属性值等于谓词值(缺省值)或包含谓词值,如docs中所述。

也就是说,替代方法是使用包含进行比较,然后使用返回的值筛选结果。

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

你可以使用上MPMediaQuerycollectionSections属性来获取数据的相关部分。对于artistsQuery,每个MPMediaQuerySectiontitle表示艺术家姓名的第一个字母。每个部分还有一个range,然后您可以申请从collections阵列获取艺术家姓名的子阵列。

这会给你的MPMediaQuerySection为信一个

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

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

然后采取部分的range属性来获取所有艺术家集合的一个子开始以字母一个

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

for (MPMediaItemCollection *artistCollection in filteredCollections) { 
    NSLog(@"%@", [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist]); 
}