如何在 Objective C 的 UILabel 中显示歌曲数?

How can I display the number of songs in a UILabel in Objective C?

我如何统计 iTunes 资料库中的歌曲数量并使用 Objective C 在 UILabel 中显示歌曲数量?

这样做就可以了:

MPMediaQuery *fullList = [[MPMediaQuery alloc] init];
//You could add a predicate to filter out what you (don't) need, for example if you don't cloud items: 
//[fullList addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]];
NSArray *mediaList = [fullList items];

UILabel *numberOfSongs = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 200)];
numberOfSongs.text = [NSString stringWithFormat:@"%li",mediaList.count];
[self.view addSubview:numberOfSongs];

要查找某个艺术家的所有歌曲,您可以这样做:

//The artist you want to find
NSString *artistToFind = @"Green day";

//Perform the query
MPMediaQuery *fullList = [[MPMediaQuery alloc] init];
[fullList addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:artistToFind forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonContains]];
NSArray *mediaList = [fullList items];

//Log the total result
NSLog(@"%@ Tracks found for artist: %@",[NSString stringWithFormat:@"%li",(unsigned long)mediaList.count],artistToFind);

//Log the individual results
for(int i=0; i<mediaList.count; i++){
    NSLog(@"Track: %@",[mediaList[i] valueForProperty:MPMediaItemPropertyTitle]);
}