我尝试将一些存储NSMutableDictionaries
到一个NSMutableArray
throw a for 循环中:
NSMutableArray *productsIdAndQuantities = [[NSMutableArray alloc]init];
NSMutableDictionary *element = [[NSMutableDictionary alloc]init];
for (id object in shoppingArray) {
[element setObject:[object valueForKey:@"proId"] forKey:@"id"];
[element setObject:[object valueForKey:@"qty"] forKey:@"quantity"];
NSLog(@"%@",element);//tested, it's different on every iteration
[productsIdAndQuantities addObject:element];
}
循环结束后,我尝试调试productsIdAndQuantities
,结果发现它包含上面循环的确切数量的项目,但只有一个值。
这就是我的意思:
循环内部(NSLog(@"%@",element);
):
{
id = 13293;
quantity = "-2";
}
{
id = 12632;
quantity = "-5";
}
循环之后(NSLog(@"%@",productsIdAndQuantities);
相同的值!!!!!!):
(
{
id = 12632;
quantity = "-5";
},
{
id = 12632;
quantity = "-5";
}
)