objective-c - NSArray 循环抓取对象

标签 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 并将其放入一个单元格中,该单元格分为两个 subview 。

我发现,通过使用上面的代码,它肯定没有这样做。有没有更简单的方法可以做到这一点,如果是这样,有人可以告诉我这一点。我真的需要通过将内存保留和时间消耗保持在最低限度来解决这个问题。

最佳答案

如果您可以对此进行预处理,那就太好了,但如果由于某种原因您不能这样做,那么您应该这样做,

- (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 循环方法可能是最好的方法。

关于objective-c - NSArray 循环抓取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6482082/

相关文章:

ios - 每天在服务器上保存数据

objective-c - 类型 "struct objc_method"的定义不完整

iphone - 构建独立的类

c++ - 无限对象

javascript - JQuery 数组重新排列 JSON 对象

ios - UISearchDisplayController 可以在开始搜索之前显示数据吗

objective-c - 切换到 Xcode7 时出现链接器问题,未定义架构 armv7 的符号 : _objc_readClassPair

iphone - UIWebView 中的字体显示不正确

iphone - 相对于 iPad 和 iPhone 放置图像

javascript - JS 中的原型(prototype)问题