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

  • SEARCH
  • Home
  • 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 8497399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:06:55+00:00 2026-06-11T00:06:55+00:00

allItems is an NSMutableArray and when the user clicks the plus button the method

  • 0

allItems is an NSMutableArray and when the user clicks the plus button the method createItem gets called. I’m trying to only add objects (of the class BNRItem) for every even number index, so I tried putting an instance of NSNull for the odd number indexes:

-(BNRItem *)createItem {
    BNRItem *p = [[BNRItem alloc] init];
    if ([allItems count] == 0)
        [allItems addObject: p];
    else {
        [allItems addObject: [NSNull null]];
        [allItems addObject: p];
    }
    return p;
}

And the output after I clicked the plus button 3 times is this:

2012-09-03 13:20:13.876 Homepwner[718:f803] Index: 0 item: Laptop (123): Worth $60, recorded on (September)
2012-09-03 13:20:13.876 Homepwner[718:f803] Index: 1 item: <null>
2012-09-03 13:20:13.877 Homepwner[718:f803] Index: 2 item: Brush (234): Worth $14, recorded on (September)
2012-09-03 13:20:13.882 Homepwner[718:f803] Index: 1 item: <null>
2012-09-03 13:20:13.882 Homepwner[718:f803] Index: 4 item: Calculator (345): Worth $19, recorded on (September)

If I keep clicking the plus button the <null> object always remains at index 1 instead of incrementing to 3, 5, and so on. I was wondering why it’s like this and how I can fix it.

  • 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-06-11T00:06:56+00:00Added an answer on June 11, 2026 at 12:06 am

    You’re not showing the code for your logging, but, based on the results, I’d say you’re doing something like:

    for( id obj in arr ){
        NSLog(@"Index: %lu item: %@", [arr indexOfObject:obj], obj);
    }
    

    The indexOfObject: method finds the lowest index where obj is equal to an item in the array. Since [NSNull null] always gives you the same object (it’s a singleton), when obj in the loop is the NSNull, indexOfObject: will always stop searching at 1.

    The for–in loop will always go through your array in order, so you can just keep a counter to print the current index:

    NSUInteger idx = 0;
    for( id obj in arr ){
        NSLog(@"Index: %lu item: %@", idx++, obj);
    }
    

    or use a “regular” for loop:

    NSUInteger count = [arr count];
    for( NSUInteger i = 0; i < count; i++ ){
        NSLog(@"Index: %lu item: %@", i, [arr objectAtIndex:i]);
    }
    

    Finally, the array will print itself and send description to each of its contained items if you just log it: NSLog(@"%@", arr);. This will be in order, so unless you really need the indexes attached, I’d suggest that.

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

Sidebar

Related Questions

I'm trying to get a list of JSON objects (products) from a local file
I have NSButton what accepts drop on to add dragged items to NSMutableArray. How
I have a list of User objects, and I have to remove ONE item
I'm trying to address all items of a list without a forEach-loop. For example:
Using redis hgetall , gets all items as a dict. How do i get
How do you Test that a IEnumerable has all items of class SomeClass in
I'm trying to print the second member variable of all items in an stl
ABAddressBookRef _addressBookRef = ABAddressBookCreate (); NSArray* allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef); NSMutableArray* _allItems = [[NSMutableArray
I have a code like this: <mx:Repeater id=allItemRepeator dataProvider={_model.allItems} > <components:ComponentSelector id=componentSelector dataLoad={allItemRepeator.currentItem} />
How to obtain document library name from the URL cqwp. For example, http:///sites/site1/DocLib/Forms/AllItems.aspx I

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.