NSArray循环抓取对象

时间:2011-06-26 04:07:42

标签: objective-c ipad object for-loop nsarray

我目前正在开发一个应用程序,它需要能够从NSArray中获取两个对象,然后将其存储在另一个对象中。

我目前正在进行这种快速枚举循环,

NSUInteger count = 0;
NSUInteger i = count + 1;
for (id item in [section items]) {


    item1 = [section.items objectAtIndex:count];
    item2 = [section.items objectAtIndex:i];

    count++;

}

现在,我想要做的是抓住第一个位置的对象并存储在item1中,然后第二个位置将存储在item2中。下次循环时,我希望它将对象存储在item1的第三个位置,然后将第二个位置存储在item2中,依此类推。

有没有人试过并实现这个目标?

修改

这就是我现在所拥有的,我认为我最好解释一下我正在做些什么,所以这里就是这样。这是我先得到的代码,之后我会解释。

MPSection *section = [self.sections objectAtIndex:indexPath.section];

NSArray *itemArray = [section items];
for (NSUInteger i = 0; (i + 1) < [section.items count]; i += 2) {
    item1 = [itemArray objectAtIndex:i];
    item2 = [itemArray objectAtIndex:i+1];
}

正如您所看到的,这是在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath内运行的,因为我想抓住通常在UITableView的第一行和第二行中显示的内容,并将其放入一个分为两个子视图的单元格中。 / p>

我发现,通过使用上面的代码,它绝对不是那样做的。有没有更简单的方法可以做到这一点,如果是这样,有人可以告诉我这个。我真的需要通过内存保存和时间消耗来保持最小化。

4 个答案:

答案 0 :(得分:3)

如果您可以对此进行预处理会很好,但如果您因某些原因无法做到这一点,那么这就是您应该做的事情,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    MPSection * section = [self.sections objectAtIndex:section];
    NSInteger   count   = [section.items count];

    return ((count % 2 == 0)? count / 2 : (count / 2 + 1) );
}

tableView:cellForRowAtIndexPath:方法中,

/* */

MPSection *section = [self.sections objectAtIndex:indexPath.section];

id item1 = [section.items objectAtIndex:(indexPath.row * 2)];
id item2 = ((indexPath.row * 2 + 1) < [section.items count])? [section.items objectAtIndex:(indexPath.row * 2 + 1)] : nil;

/* Use item1 & item2 to fill both the subviews */

原始答案

为此目的使用NSEnumerator实例

NSEnumerator *enumerator = [section.items objectEnumerator];
id item1, item2;
while ( (item1 = [enumerator nextObject]) && (item2 = [enumerator nextObject]) ) {
    // Use item1 & item2
}

因此我认为您必须为您提到的代码段获取索引超出范围的错误。

<强>过度破坏

似乎有一些关于性能的问题,所以我测试了三种建议的方法,并将它们定时在我记录它们的循环中。

Enumerator, Fast Enumeration with Object Search, For Loop (50000 elements): 19.253626, 88.269961, 18.767572
Enumerator, Fast Enumeration with Object Search, For Loop (25000 elements): 9.164311, 25.105664, 8.777443
Enumerator, Fast Enumeration with Object Search, For Loop (10000 elements): 3.428265, 6.035876, 3.144609
Enumerator, Fast Enumeration with Object Search, For Loop (5000 elements): 2.010748, 2.548562, 1.980477
Enumerator, Fast Enumeration with Object Search, For Loop (1000 elements): 0.508310, 0.389402, 0.338096
Enumerator, Fast Enumeration with Object Search, For Loop (500 elements): 0.156880, 0.163541, 0.150585
Enumerator, Fast Enumeration with Object Search, For Loop (100 elements): 0.076625, 0.034531, 0.036576
Enumerator, Fast Enumeration with Object Search, For Loop (50 elements): 0.026115, 0.022686, 0.041745

从它的外观来看,@ Caleb的for循环方法可能是最好的方法。

答案 1 :(得分:3)

如果你在这里使用快速枚举,你可以设置一个标志,让你跳过循环的每个偶数迭代:

BOOL doThisOne = YES;
NSArray itemArray = [section itemArray];
for (id item in items) {
    if (doThisOne) {
        item1 = item;
        item2 = [itemArray objectAtIndex:1+[itemArray indexOfObject:item]];
    }
    doThisOne = !doThisOne;
}

注意:如果数组中的项目数为奇数,则上面的代码会抛出范围异常。其他一些答案可以避免这种情况,但我认为最好的答案就是不要在这里使用快速枚举。

使用枚举器要简单得多,或者只使用常规的旧for循环:

NSEnumerator *e = [[section items] objectEnumerator];
while (item = [e nextObject]) {
    item1 = item;
    item2 = [e nextObject];
}

或:

NSArray *itemArray = [section items];
for (int i = 0; (i + 1) < [items count]; i += 2) {
    item1 = [items objectAtIndex:i];
    item2 = [items objectAtIndex:i+1];
}

答案 2 :(得分:1)

for(id item in array){
    if([array indexOfObject:item] % 2 != 0){
     item1 = item;
    }else{
     item2 = item;
    }
}

答案 3 :(得分:1)

int numItems = [section.items count];

for(int i = 0; i < count; i++) {
    item1 = [section.items objectAtIndex: i];
    item2 = ((i + 1) < count)) ? [section.items objectAtIndex: (i + 1)] : nil;
}