Ios 循环基本数组,知道总计数

Ios 循环基本数组,知道总计数,ios,objective-c,Ios,Objective C,我正在尝试转换这样的数组 _Array = @[[[feeds objectAtIndex:0] objectForKey: @"title"], [[feeds objectAtIndex:1] objectForKey: @"title"], [[feeds objectAtIndex:2] objectForKey: @"title"], [[feeds objectAtIndex:3] objectForKey: @"ti

我正在尝试转换这样的数组

_Array = @[[[feeds objectAtIndex:0] objectForKey: @"title"],
           [[feeds objectAtIndex:1] objectForKey: @"title"],
           [[feeds objectAtIndex:2] objectForKey: @"title"],
           [[feeds objectAtIndex:3] objectForKey: @"title"],
           [[feeds objectAtIndex:4] objectForKey: @"title"],
           [[feeds objectAtIndex:5] objectForKey: @"title"],
           [[feeds objectAtIndex:6] objectForKey: @"title"],
           [[feeds objectAtIndex:7] objectForKey: @"title"],
           [[feeds objectAtIndex:8] objectForKey: @"title"],
           [[feeds objectAtIndex:9] objectForKey: @"title"],
           [[feeds objectAtIndex:10] objectForKey: @"title"],

           [[feeds objectAtIndex:xx] objectForKey: @"title"]];
进入一个循环。我知道数组的总数。一直在尝试这样的事情

for (int i = 0; i < dCount; i++) {
    [[feeds objectAtIndex:i] objectForKey: @"title"];

      }
但不确定如何将循环添加到数组中


谢谢

我想你要问的是如何使用循环向_数组添加元素,对吗

假设_数组是NSArray并且提要包含字典,请尝试此操作:

NSMutableArray *tmpArray = [NSMutableArray array];
for (NSDictionary *dict in feeds) {
    [tmpArray addObject:dict[@"title"]];
}

_Array = [tmpArray copy]; // make it immutable
使用KVC更简单的方法是:

_Array = [feeds valueForKey:@"title"];
顺便说一句,标准命名约定规定变量和方法名称应以小写字母开头。考虑将数组转换成数组。< /P> 最新评论:

您需要使用类似的循环作为我答案的第一部分:

NSMutableArray *tmpArray = [NSMutableArray array];
for (NSDictionary *dict in feeds) {
    NSString *imagePath = [[empty stringByAppendingPathComponent:dict[@"image"]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    [tmpArray addObject:imagePath];
}
_images = [tmpArray copy];

@乔希:我不确定我是否同意这个复制品。这个问题是问如何使用循环。副本询问如何在没有循环的情况下执行。虽然这个问题可以在没有循环的情况下解决,就像在我的答案和副本中一样,但这个问题并没有问这个问题。因为我知道循环并不是最好的解决方法,所以我可能在解决问题方面犯了错误,而不是在这里回答问题,@rmaddy;如果你想重新打开它,我不反对。这很好地解决了上面的问题,非常感谢mate,但也产生了一些其他问题,我如何将你的方法用于这样的数组_images=@[[empty stringByAppendingPathComponent:[[feeds objectAtIndex:0]objectForKey:@image]]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]],请查看我的更新以处理_images数组。您是我的英雄。谢谢rmaddy。对于其他人,您可能需要编辑addObject:image to addObject:imagePath