0

我想在 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);
    }
});
4

1 回答 1

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!
}
于 2012-08-20T09:07:53.657 回答