rockey
rockey

Reputation: 638

arrays are not adding to the nsmutabledictionary

i am adding the arrays to an nsmutabledictionary but its giving 0 count i allocated the dictionary in viewDidLoad method.

can any body help me out please..

thanks.

- (void)creatingDictionary{

        songsDict = nil;
        NSMutableArray *hrSongs = [[NSMutableArray alloc]init];
        for(int i=0;i<[onAirSongs count];i++){
            for(int j=i;j<[onAirSongs count];j++){
                Song *a1 =(Song *)[onAirSongs objectAtIndex:i];
                Song *a2 =(Song *)[onAirSongs objectAtIndex:j];
                if(a1.cellId == a2.cellId)
                    [hrSongs addObject:a2];
                else{
                    [songsDict setObject:hrSongs forKey:[NSString stringWithFormat:@"%d",i]];                        
                    hrSongs = nil;
                    i=j-1;
                    break;
                }
            }//inner for
        }//out for
        [hrSongs release];
        NSLog(@"songsdictionary count....%d",[songsDict count]);

    }

Upvotes: 0

Views: 179

Answers (2)

bbum
bbum

Reputation: 162722

blindJesse is correct, but there are more problems here:

  1. You are setting songsDict to nil. In Objective-C, nil-eats-messages and, thus, you are silently not doing anything at all when you [songsDict setObject:.....].

  2. In the middle of the loop, you hrSongs = nil;. Subsequent iterations through the loop will happily call addObject: on nil, doing nothing at all.

  3. This whole thing seems kinda weird; what does onAirSongs contain? Cells? This seems like you haven't really preserved any kind of Model-View-Controller separation. It might work, but you are in for a severe maintenance headache down the road if you ever have to change your UI.

Upvotes: 1

Jesse Anderson
Jesse Anderson

Reputation: 4613

You're setting songsDict to nil. You can't add an array to a dictionary that doesn't exist.

Upvotes: 3

Related Questions