我想在iOS中读取一个数据数组,但我只读取了这个数组中的一条记录,我检查了数组,它包含10个项目。
for(NSString *po in list) {
infor.poster = [po valueForKey:@"Poster"];
infor.vName= [po valueForKey:@"VName"];
infor.name = [po valueForKey:@"Name"];
infor.HDtrailerPath = [po valueForKey:@"HDTrailerPath"];
[arrfilm addObject:infor];
}
dispatch_async(dispatch_get_main_queue(), ^{
int widthlblscroll = 0;
for (Inforfilm *u in arrfilm) {
Inforfilm *infoF = [[Inforfilm alloc]init];
infoF.poster = [u valueForKey:@"poster"];
infoF.name = [u valueForKey:@"name"];
NSLog(@"%@",infoF.poster);
NSLog(@"%@",infoF.name);
}
});
答案 0 :(得分:3)
您正在向阵列多次添加Inforfilm
的同一个实例。仅仅因为您更新其属性并不意味着它是一个新实例。为了防止这种情况,请在每次循环迭代中实例化一个新实例。就像在第一行内部一样。
for(NSString *po in list)
{
//create a new instance of Inforfilm
Inforfilm *infor = [[Inforfilm alloc] init];
infor.poster = [po valueForKey:@"Poster"];
infor.vName= [po valueForKey:@"VName"];
infor.name = [po valueForKey:@"Name"];
infor.HDtrailerPath = [po valueForKey:@"HDTrailerPath"];
[arrfilm addObject:infor];
//make sure you got ARC enabled, otherwise this object will now leak!
}