Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6061613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:57:26+00:00 2026-05-23T08:57:26+00:00

I’m currently in the development process of an application which needs to be able

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:57:26+00:00Added an answer on May 23, 2026 at 8:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.