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 8381031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:36:28+00:00 2026-06-09T16:36:28+00:00

I have a NSMutable array with elements inserted like this: [availableSeatsArray addObject:[NSString stringWithString:num_available]]; I

  • 0

I have a NSMutable array with elements inserted like this:

[availableSeatsArray addObject:[NSString stringWithString:num_available]];

I want to remove the elements in the array that have values of zero or lower. I tried checking the element’s value using its int value and even its string value, but it always passes the ‘0’ element case. Console output of the array before and after below.

for (int i=0;i<[availableSeatsArray count]; i++) {
        if ([[availableSeatsArray objectAtIndex:i] intValue] <= 0 || ([[availableSeatsArray objectAtIndex:i] isEqualToString:@"0"])) {
            NSLog(@"Removed index: %d", [[availableSeatsArray objectAtIndex:i] intValue]);
            [availableSeatsArray removeObjectAtIndex:i];
        }
}

Console output:

Available array: (
    "-2",
    10,
    5,
    "-5",
    0,
    10,
    10,
)
2012-08-14 11:13:28:002 -[dmbAddReservation viewWillAppear:] [Line 1074] Removed index: -2
2012-08-14 11:13:28:004 -[dmbAddReservation viewWillAppear:] [Line 1074] Removed index: -5
2012-08-14 11:13:28:006 -[dmbAddReservation viewWillAppear:] [Line 1083] Available array: (
    10,
    5,
    0, // I cannot explain why this element was not removed
    10,
    10,
)
  • 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-09T16:36:29+00:00Added an answer on June 9, 2026 at 4:36 pm

    The problem is that this approach uses a fundamentally flawed logic. It erronously doesn’t remove consecutive occurrences of 0 or negative objects. The reason is, when in the for loop you check for “-5”, it passes the test, then you remove it, shrinking the array, and shifting its remaining elements so that the “0” will now be in the place of the “-5”. But in the for loop, you advance the loop variable (i in this case) regardless of whether an element was or wasn’t removed, so now ‘i’ points one past the zero. And it wouldn’t be checked. Solution: only increase the loop variable if there are no consecutive elements that pass your test (i. e. change the if to while):

    for (int i = 0; i < [availableSeatsArray count]; i++) {
        while ([[availableSeatsArray objectAtIndex:i] intValue] <= 0
           || ([[reservatiomAvailableArray objectAtIndex:i] isEqualToString:@"0"])) {
            NSLog(@"Removed index: %d", [[availableSeatsArray objectAtIndex:i] intValue]);
            [availableSeatsArray removeObjectAtIndex:i];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a NSMutableArray consisting of several elements. From this array I want to
I want to store my CGPoint to the NSMutable Array, so , I have
I have an NSMutable array of N elements. Each element is also an NSMutable
I have an NSMutableArray something like this array =[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3].......] and I access this array
I have a NSMutableArray and a NSArray . Both consist of elements that are
I have got an array, I know how to count its elements, but I
I have an id<NSFastEnumeration> object. I want to count the elements inside the object.
When you have an NSArray and you want to evaluate and change the elements,
I have NSMutableArray* names , which contains NSStrings. I want to sort the array
I have an NSMutableArray of IBOutlet UIImageView elements and I want to bind each

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.