我有一个应用程序,我正在尝试创建一个nzzyaries.n的nsmutablearray工作正常,但问题是最后一个元素是添加coredata数组的计数时间。这是我如何做到这一点`< / p>
rateValues=[[NSMutableArray alloc]init];
rateValues1=[[NSMutableDictionary alloc]init];
if(messagesArray!=nil)
{
for (Messagestable *rating in messagesArray)
{
//[rateValues addObject:rating.message];
[rateValues1 setObject:rating.fromid forKey:@"fromid"];
[rateValues1 setObject:rating.toid forKey:@"toid"];
[rateValues1 setObject:rating.sendingtime forKey:@"sendingtime"];
[rateValues1 setObject:rating.message forKey:@"message"];
[rateValues1 setObject:rating.fromname forKey:@"fromname"];
[rateValues addObject:rateValues1];
}
}
else{
//NSLog(@"array empty");
return;
}
NSLog(@"%@",rateValues);
`我的数组只包含最后一个元素4次。它是coredata中数据的计数。任何人都可以帮助我吗?
答案 0 :(得分:2)
每次迭代都需要创建一个新的字典;你目前正在做的是覆盖你的单个字典并将其重新添加到数组中。
rateValues=[[NSMutableArray alloc]init];
if(messagesArray!=nil)
{
for (Messagestable *rating in messagesArray)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[rateValues1 setObject:rating.fromid forKey:@"fromid"];
[rateValues1 setObject:rating.toid forKey:@"toid"];
[rateValues1 setObject:rating.sendingtime forKey:@"sendingtime"];
[rateValues1 setObject:rating.message forKey:@"message"];
[rateValues1 setObject:rating.fromname forKey:@"fromname"];
[rateValues addObject:dict];
}
}
else{
//NSLog(@"array empty");
return;
}
NSLog(@"%@",rateValues);