簡體   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