NSArray循环来抓取对象 [英] NSArray Loop to grab Objects

查看:205
本文介绍了NSArray循环来抓取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



I现在已经有了这个快速的枚举循环,

pre $ NS $ Integer count = 0;
NSUInteger i = count + 1;
([item item]中的id项){


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

count ++;






现在,我想要做的是抓住对象第一个位置并存储在item1中,然后第二个位置将存储在item2中。下一次它通过循环,我想它将对象存储在项目1中的第三个位置,然后在项目2中的第四个位置等等。

有人曾经试过并取得过这样的成绩吗?

编辑

目前有,我认为我最好解释我在做的更深一点,所以在这里。这是我第一次的代码,我会解释之后。

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

NSArray * itemArray = [部分项目]; (NSUInteger i = 0;(i + 1)< [section.items count]; i + = 2){
item1 = [itemArray objectAtIndex:i];

item2 = [itemArray objectAtIndex:i + 1];





$ b

正如你所看到的,这是在( UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ,因为我想抓住通常显示在UITableView的第一和第二行,并把它放到一个单元格分为两个子视图。



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

解决方案

如果你可以预处理这个,但如果你不能做到这一点,那么这是你应该做的,

   - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
MPSection * section = [self.sections objectAtIndex:section];
NSInteger count = [section.items count];
$ b return((count count%2 == 0)?count / 2:(count / 2 + 1));

$ / code>

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;

/ *使用item1& item2填写子视图* /



原始答案



为此目的使用 NSEnumerator 实例

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





$ b

因此,我认为你必须得到索引超出界限的错误你提到的片段。

过度杀毒

所以我测试了三个建议的方法,并在一个循环中记录它们的时间。

 枚举器,快速枚举与对象搜索, For Loop(50000 elements):19.253626,88.269961,18.767572 
枚举器,快速枚举与对象搜索,For循环(25000元素):9.164311,25.105664,8.777443
枚举器,快速枚举与对象搜索,For循环(10000个元素):3.428265,6.035876,3.144609
枚举器,快速枚举与对象搜索,For循环(5000元素):2.010748,2.548562,1.980477
枚举器,快速枚举与对象搜索,For循环(1000元素):0.508310,0.389402,0.338096
枚举器,对象搜索快速枚举,For循环(500个元素):0.156880,0。 (100个元素):0.076625,0.034531,0.036576
枚举器,快速枚举与对象搜索,For循环(50个元素):0.026115,0.022686, 0.041745

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


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.

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. 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.

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.

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,

/* */

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 *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.

这篇关于NSArray循环来抓取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆