简体   繁体   English

NSArray循环抓取对象

[英]NSArray Loop to grab Objects

I'm currently in the development process of an application which needs to be able to grab two objects from an NSArray and then store it in another object. 我目前正处于一个应用程序的开发过程中,该应用程序需要能够从NSArray中获取两个对象,然后将其存储在另一个对象中。

I've currently got this fast enumeration loop going on, 我现在有这个快速枚举循环,

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


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

    count++;

}

Now, what I want to do is grab the object in the first position and store in item1, and then the second position will be stored in item2. 现在,我想要做的是抓住第一个位置的对象并存储在item1中,然后第二个位置将存储在item2中。 The next time it goes through the loop, I want it to store the object in the third position in item1, and then the fourth position in item2 and so forth. 下次循环时,我希望它将对象存储在item1的第三个位置,然后将第二个位置存储在item2中,依此类推。

Has anyone ever tried to and achieved this? 有没有人试图实现这一目标?

EDIT 编辑

This is what I currently have, I thought it best that I explain what I'm doing a little deeper so here goes. 这就是我现在所拥有的,我认为我最好解释一下我做得更深一点,所以这里有。 Here's the code that I have first, and I'll explain afterwards. 这是我先得到的代码,之后我将解释。

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];
}

As you can see, this is running within (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath as I want to grab what would normally be displayed in the first and second row of a UITableView and put it into one cell which is divided into two subviews. 正如你所看到的,这是在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath因为我想抓住通常在UITableView的第一行和第二行显示的内容并将其放入一个细胞分为两个子视图。

What I'm finding is, by using the above code, it definitely isn't doing that. 我发现,通过使用上面的代码,它绝对不是那样做的。 Is there a simpler way that I can do this and if so, can someone please inform me about this. 有没有更简单的方法可以做到这一点,如果是这样,有人可以告诉我这个。 I really need to approach this with memory preservation and time consumption kept to a minimal as well. 我真的需要通过内存保存来解决这个问题,并且时间消耗也保持在最低限度。

It would be good if you can preprocess this but if you can't do that for some reason then this is what you should do, 如果你可以预处理它会很好但是如果由于某些原因你不能这样做那么这就是你应该做的,

- (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) );
}

And in tableView:cellForRowAtIndexPath: method, 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 */

Original Answer 原始答案

Use the NSEnumerator instance for this purpose 为此,请使用NSEnumerator实例

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

As such I think you must be getting index out of bounds error for the snippet you mentioned. 因此,我认为您必须为您提到的代码段获取索引超出范围的错误。

Overkill 矫枉过正

There seems to be some question about performance so I tested the three suggested methods and timed them in a loop where I log them. 似乎有一些关于性能的问题,所以我测试了三种建议的方法,并将它们定时在我记录它们的循环中。

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

From the looks of it, @Caleb's for loop approach might be the best approach to take. 从它的外观来看,@ Caleb的for循环方法可能是最好的方法。

If you're dead set on using fast enumeration here, you could set a flag that lets you skip each even iteration of the loop: 如果你在这里使用快速枚举,你可以设置一个标志,让你跳过循环的每个偶数迭代:

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;
}

Note: The code above throws a range exception if the number of items in the array is odd. 注意:如果数组中的项目数为奇数,则上面的代码会引发范围异常。 Some of the other answers avoid this, but I think the best answer is simply not to use fast enumeration here. 其他一些答案可以避免这种情况,但我认为最好的答案就是不要在这里使用快速枚举。

It'd be so much simpler to use an enumerator, or just use a regular old for loop: 使用枚举器要简单得多,或者只使用常规的旧for循环:

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

or: 要么:

NSArray *itemArray = [section items];
for (int i = 0; (i + 1) < [items count]; i += 2) {
    item1 = [items objectAtIndex:i];
    item2 = [items objectAtIndex:i+1];
}
for(id item in array){
    if([array indexOfObject:item] % 2 != 0){
     item1 = item;
    }else{
     item2 = item;
    }
}
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;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM