Carsten Graf
Carsten Graf

Reputation: 63

Values changing automatically

Hi together I have a Problem: I read out the actual Values of the Playing media song, and add it to a dictionary. And then into an Array. If I add a new entry pressing the Button again, all Values of all entrys changing to the same. Do you have an Idea why?

Here my Code:

    - (IBAction)ActionButtonLU:(id)sender
{
    MPMediaItem *currentItem = [musicPlayer nowPlayingItem];

    NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
    NSString *artistString = [currentItem valueForProperty:MPMediaItemPropertyArtist];
    NSString *albumString = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];

    if (titleString == nil) {
        titleString = @"";
    }
    if (artistString == nil) {
        artistString = @"";
    } 
`  `if (albumString == nil) {
        albumString = @"";
    }

    [_dictCat1 setObject:titleString forKey:@"Titel"];
    [_dictCat1 setObject:artistString forKey:@"Artist"];
    [_dictCat1 setObject:albumString forKey:@"Album"];

    [_countCat1 addObject:_dictCat1];

    [musicPlayer skipToNextItem];

}

Upvotes: 0

Views: 57

Answers (2)

Kreiri
Kreiri

Reputation: 7850

When you add an object to a collection (NSArray, NSDictionary, NSSet etc, including mutable counterparts), the collection doesn't copy it. With [_countCat1 addObject:_dictCat1]; you keep adding the same object to _countCat1 over and over. You need to create new NSDictionary and add that to _countCat1 collection.

Upvotes: 0

Joe
Joe

Reputation: 57179

You are modifying the same dictionary, _dictCat1, in every call. You need to create a dictionary locally and add that to _countCat1.

[_countCat1 addObject:@{ @"Title": titleString, 
                         @"Artist": artistString, 
                         @"Album": albumString }];

Upvotes: 1

Related Questions