Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:46:11+00:00 2026-06-05T15:46:11+00:00

That’s my Question =) MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery]; NSArray *songsByArtist = [artistQuery collections];

  • 0

That’s my Question =)

MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
NSArray *songsByArtist = [artistQuery collections];

How can I get the number of albums of each artists of the MPMediaItemCollections in songsByArtist ?

Exemple :

The Beatles
3 Albums

AC/DC
6 Albums

Thank You !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:46:13+00:00Added an answer on June 5, 2026 at 3:46 pm

    The artistsQuery convenience constructor does not sort and group by album. artistsQuery returns an array of media item collections of all artists sorted alphabetically by artist name. Nested inside each artist collection is an array of media items associated with all songs for that artist. The nested array is sorted alphabetically by song title.

    One way to keep a count of albums by artist is to enumerate through all the song items for each artist collection and use a NSMutableSet to keep track of distinct album titles associated with each song. Then add the count of the set as the value for each artist key in a NSMutableDictionary. Any duplicate album titles will not be added since a NSMutableSet will only take distinct objects:

    MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
    NSArray *songsByArtist = [artistQuery collections];
    NSMutableDictionary *artistDictionary = [NSMutableDictionary dictionary];
    NSMutableSet *tempSet = [NSMutableSet set];
    
    [songsByArtist enumerateObjectsUsingBlock:^(MPMediaItemCollection *artistCollection, NSUInteger idx, BOOL *stop) {
        NSString *artistName = [[artistCollection representativeItem] valueForProperty:MPMediaItemPropertyArtist];
    
        [[artistCollection items] enumerateObjectsUsingBlock:^(MPMediaItem *songItem, NSUInteger idx, BOOL *stop) {
            NSString *albumName = [songItem valueForProperty:MPMediaItemPropertyAlbumTitle];
            [tempSet addObject:albumName];
        }];
        [artistDictionary setValue:[NSNumber numberWithUnsignedInteger:[tempSet count]] 
                            forKey:artistName];
        [tempSet removeAllObjects];
    }];
    NSLog(@"Artist Album Count Dictionary: %@", artistDictionary);
    

    It would be cleaner if you change the query to albumsQuery. This query groups and sorts the collection by album name. Then it is just a matter of enumerating through the array of album collections and keeping a count of the representative artist name for each album in a NSCountedSet. The counted set will track the number of times objects are inserted:

    MPMediaQuery *albumQuery = [MPMediaQuery albumsQuery];
    NSArray *albumCollection = [albumQuery collections];
    NSCountedSet *artistAlbumCounter = [NSCountedSet set];
    
    [albumCollection enumerateObjectsUsingBlock:^(MPMediaItemCollection *album, NSUInteger idx, BOOL *stop) {
        NSString *artistName = [[album representativeItem] valueForProperty:MPMediaItemPropertyArtist];
        [artistAlbumCounter addObject:artistName];
    }];
    NSLog(@"Artist Album Counted Set: %@", artistAlbumCounter);
    

    You can also retrieve the count for a given object in a NSCountedSet with the countForObject: method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That is the question. Background: C# Params In C#, you can declare the last
That's it. How can I get the old value of a textarea in order
That's pretty much what I am trying to accomplish. To have each image appear
That might sound silly but can't do it myself :( Need your help guys.
That show up on menu bar It's like this question: How to create a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's the best way I could think of to phrase my question, here is
That is a broad question but I want to listen to your opinion. Three
That the razor syntax is neat, there's little arguing about. But i can't seem
That's my question, and here's my example code. HTML: <a class=fancybox rel=group href=img1.jpg> <img

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.