我可以在for循环中为NSarray或字典添加键值对吗?

时间:2013-04-09 06:03:04

标签: ios plist nsdictionary nsmutabledictionary

我想从带有键值对的for循环中向NSMutabledictionary添加值吗?

目前正在使用此代码

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];
            }
     }
}

结果是每次最终字典“m”的值被最新值替换,但我想要所有值都是这样的吗?

enter image description here我试图获得像这样的输出

3 个答案:

答案 0 :(得分:2)

我想你可以将字典的初始化移到for之外,因为它已经在每个循环中初始化了,这就是为什么它只有一个值

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];
    }
}

<强>更新

我已更新上面的代码,以便调整图片中的输出

答案 1 :(得分:0)

试试这段代码。

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];
        }
    }

我认为你每次在内部循环中分配一个新的字典,这是不需要的。分配一次&amp;然后继续为该可变字典添加值。

我认为,每次你有不同的catId作为关键

答案 2 :(得分:0)

在你的循环中,请尝试这个逻辑

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