How do I add object to NSMutableDictionary with key?
Asked Answered
P

2

6

I have a plist. I want add the elememts from that plist to a mutable dictionary. First I check the user name in the plist, and my current name are same or not. If it is same, then I want to add all value to that dictionary rom plist, and also assign the same key:

 for(int i=0;i<[displayList count];i++)
    {
        if([[[data valueForKey:@"username"] objectAtIndex:i] isEqualToString:user])
        {
            [finalBooks setValue:[[data valueForKey:@"ID"] objectAtIndex:i] forKey:@"ID"];
            [finalBooks setValue:[[data valueForKey:@"name"] objectAtIndex:i] forKey:@"name"];
            [finalBooks setValue:[[data valueForKey:@"Place"] objectAtIndex:i] forKey:@"Place"];
          [finalBooks setValue:[[data valueForKey:@"username"] objectAtIndex:i] forKey:@"username"];
            

        }
    }

[displayList count] this value is 7 so each value replacing 6 times ,and am getting only final values I want every values matching to user name I that mutabledictionary.

Peppi answered 20/5, 2013 at 6:48 Comment(4)
so what's the problem here? hopefully "finalBooks" is your "NSMutableDictionary", yes?Inflammable
yes but [displayList count] this value is 7 so each value replacing 6 times ,and am getting only final valuesPeppi
@mango this is because you are setting (replacing) values of dictionary against same keys in every iteration, thats why you only get final values.Flivver
Break at least the first of those lines into 3 separate lines so you can see what's going on.Glazier
G
15

I think you should create array of dictionary. Just add your finalBooks dictionary to NSMutableArray like this

NSMutableArray *finalArray = [[NSMutableArray alloc]init];

(int i=0;i<[displayList count];i++)
{
    if([[[data valueForKey:@"username"] objectAtIndex:i] isEqualToString:user])
    {
        [finalBooks setValue:[[data valueForKey:@"ID"] objectAtIndex:i] forKey:@"ID"];
        [finalBooks setValue:[[data valueForKey:@"name"] objectAtIndex:i] forKey:@"name"];
        [finalBooks setValue:[[data valueForKey:@"Place"] objectAtIndex:i] forKey:@"Place"];
        [finalBooks setValue:[[data valueForKey:@"username"] objectAtIndex:i] forKey:@"username"];

        [finalArray addObject:finalBooks];
    }
}

And you will get array of dictionary. Good Luck !!

Got answered 20/5, 2013 at 7:16 Comment(1)
Shouldn't you use -objectForKey:/-setObject:forKey:?Chiron
A
3

setValue method in a NSMutableDictionary replaces the value if the key is same. So what you can do is either use NSMutableDictionary of arrays or dictionaries.

NSMutableDictionary *userDictionary=[[NSMutableDictionary alloc]init];

(int i=0;i<[displayList count];i++)
{
    if([[[data valueForKey:@"username"] objectAtIndex:i] isEqualToString:user])
    {
        [finalBooks setValue:[[data valueForKey:@"ID"] objectAtIndex:i] forKey:@"ID"];
        [finalBooks setValue:[[data valueForKey:@"name"] objectAtIndex:i] forKey:@"name"];
        [finalBooks setValue:[[data valueForKey:@"Place"] objectAtIndex:i] forKey:@"Place"];
        [finalBooks setValue:[[data valueForKey:@"username"] objectAtIndex:i] forKey:@"username"];

        [userDictionary setObject:finalBooks forKey:user];
    }
}

If you want everything in one dictionary, I suggest you use a unique key such as NSString stringWithFormat:@"ID+%@",username],...[NSString stringWithFormat:@"username+%@",username]

But it looks messy :)

Acquaintance answered 20/5, 2013 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.