Naveen
Naveen

Reputation: 1251

can i add key value pair to NSarray or dictionary inside a for loop?

i want to add values to NSMutabledictionary from a for loop with key value pairs ?

Currently am using this code

for(int j=0;j<[array count];j++){
    for(int l=0;l<array2 count] ;l++){   
          // retreive the category from plist ,then check whether its matching with "catid"
          NSString *temp=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"cate"][l][@"categories"]];

           if([catId isEqualToString:temp]){    
                 // "catid" is matching with temp then ,retreive the corresponding bid from plist
                 NSString *str=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"bid"]];
                 // add that  value "bid" and key "catid" to a mutabledictionary
                 NSMutableDictionary *m=[[NSMutableDictionary alloc]init];
                 [m setValue:str forKey:catId];
            }
     }
}

The result is each time the value of the final dictionary "m" get replaced by latest values,but i want all values toghether?

enter image description here iam trying to get an output like this

Upvotes: 0

Views: 5533

Answers (3)

Binoy jose
Binoy jose

Reputation: 481

Inside your loop please try this logic

NSObject *collectionIndexString = indexPath;
NSMutableDictionary *tempDict = [myNSMutableArray[row] mutableCopy];
tempDict[@"collectionIndex"] = collectionIndexString;// Setting Key and Value
[myNSMutableArray replaceObjectAtIndex:row withObject:tempDict];

Upvotes: 0

alexcristea
alexcristea

Reputation: 2366

I suppose you can move the initialization of the dictionary outside of the for, because in for it got initialised in every loop and that's why it got only one value

NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
for(int j=0; j < [array count]; j++)
{
    NSMutableArray *containerArray = [[NSMutableArray alloc] init];
    for(int l=0;l<[array2 count] ;l++)
    {
        // retreive the category from plist ,then check whether its matching with "catid"
        NSString *temp=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"cate"][l][@"categories"]];

        if([catId isEqualToString:temp])
        {
            // "catid" is matching with temp then ,retreive the corresponding bid from plist
            NSString *str=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"bid"]];

            // add that  value "bid" and key "catid" to a mutabledictionary
             [containerArray addObject:str];
        }
        [m setValue:containerArray forKey:catId];
    }
}

UPDATE

I have updated the code above in order to adapt your output in picture

Upvotes: 2

hp iOS Coder
hp iOS Coder

Reputation: 3234

Try this code.

NSMutableDictionary *m=[[NSMutableDictionary alloc]init];
for(int j=0;j<[array count];j++)
{
    for(int l=0;l<array2 count] ;l++)
    {
        // retreive the category from plist ,then check whether its matching with "catid"
        NSString *temp=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"cate"][l][@"categories"]];
        if([catId isEqualToString:temp])
        {
            // "catid" is matching with temp then ,retreive the corresponding bid from plist
            NSString *str=[NSString stringWithFormat:@"%@",allBookPlist[@"listbook"][j][@"bid"]];
            // add that  value "bid" and key "catid" to a mutabledictionary
            [m setValue:str forKey:catId];
        }
    }

I think you are allocating a new dictionary each time inside loop which is not required. Allocate it once & then just keep on adding values to that mutable dictionary.

I presume, each time you have different catId as a KEY

Upvotes: 0

Related Questions