0

キーと値のペアを使用して 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」の値が最新の値に置き換えられるたびに得られますが、すべての値をまとめたいですか?

ここに画像の説明を入力このような出力を得ようとしています

4

3 に答える 3

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

必要のないループ内で毎回新しい辞書を割り当てていると思います。一度割り当ててから、その変更可能な辞書に値を追加し続けます。

catId私は、あなたがキーとして異なるたびに推測します

于 2013-04-09T06:07:56.533 に答える
0

ループ内でこのロジックを試してください

NSObject *collectionIndexString = indexPath;
NSMutableDictionary *tempDict = [myNSMutableArray[row] mutableCopy];
tempDict[@"collectionIndex"] = collectionIndexString;// Setting Key and Value
[myNSMutableArray replaceObjectAtIndex:row withObject:tempDict];
于 2016-07-26T19:57:05.453 に答える