ios - can i add key value pair to NSarray or dictionary inside a for loop? -
i want add values nsmutabledictionary loop key value pairs ?
currently using code
for(int j=0;j<[array count];j++){ for(int l=0;l<array2 count] ;l++){ // retreive category plist ,then check whether matching "catid" nsstring *temp=[nsstring stringwithformat:@"%@",allbookplist[@"listbook"][j][@"cate"][l][@"categories"]]; if([catid isequaltostring:temp]){ // "catid" matching temp ,retreive corresponding bid plist nsstring *str=[nsstring stringwithformat:@"%@",allbookplist[@"listbook"][j][@"bid"]]; // add value "bid" , key "catid" mutabledictionary nsmutabledictionary *m=[[nsmutabledictionary alloc]init]; [m setvalue:str forkey:catid]; } } }
the result each time value of final dictionary "m" replaced latest values,but want values toghether?
iam trying output this
i suppose can move initialization of dictionary outside of for, because in got initialised in every loop , that's why got 1 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 category plist ,then check whether matching "catid" nsstring *temp=[nsstring stringwithformat:@"%@",allbookplist[@"listbook"][j][@"cate"][l][@"categories"]]; if([catid isequaltostring:temp]) { // "catid" matching temp ,retreive corresponding bid plist nsstring *str=[nsstring stringwithformat:@"%@",allbookplist[@"listbook"][j][@"bid"]]; // add value "bid" , key "catid" mutabledictionary [containerarray addobject:str]; } [m setvalue:containerarray forkey:catid]; } }
update
i have updated code above in order adapt output in picture
Comments
Post a Comment