繁体   English   中英

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

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

我想从一个带有键值对的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”的值被最新值替换,但我想要所有值都是?

在此输入图像描述 我试图获得这样的输出

我想你可以将字典的初始化移到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];
    }
}

UPDATE

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

试试这个代码。

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM