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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:58:17+00:00 2026-06-06T13:58:17+00:00

I have this method that gets called when i press a button and removes

  • 0

I have this method that gets called when i press a button and removes a Object from the MutableArray favs. Since it leaves holes in the Array I run into problems later. Is there an easy way to remove those “holes”, or a better way to remove Objects?

- (IBAction)addfavs:(id)sender {

int erase;
for(int i=0;i<favs.count;i++){

    if ([favs objectAtIndex:i] == parent) {

        [favs removeObjectAtIndex:i];
        erase = 1;
    }
}
if (!erase) {
    [favs addObject:parent];
}

}

  • 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-06T13:58:18+00:00Added an answer on June 6, 2026 at 1:58 pm

    [EDIT4]

    Great point by user523234

    [favs removeObjectIdenticalTo: parent];
    

    Look at removeObjectIdenticalTo at Apples site for NSMutableArray

    https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

    [EDIT3]

    Here is an example of checking with the value (using an integer value), instead of the object itself.

    int erase = 0;
    int i = 0;
    while(i < favs.count){
        if ([[favs objectAtIndex:i] intValue] == [parent intValue]){
            [favs removeObjectAtIndex:i];
            erase = 1;
        }else{
            i++;
        }
    }
    

    [EDIT2]

    The reason it may add an object even if it doesnt remove first is because you never reset your “erase” variable. The first time you set it to 1, it is set for the rest of the time!!! You also never initialize it, thus you never “really” know what the compiler is going to do with the variable the NEXT time your action is called. If it “allocates” the same memory address to your erase variable the “1” will still be resident creating a static condition even though you havent defined it as a static variable!!!

    [EDIT]

    Didnt originally understand your question. I think I understand now, taking a better look at your code.

    Your Code:

    int erase = 0; 
    for(int i=0;i<favs.count;i++){
        if ([favs objectAtIndex:i] == parent){
            [favs removeObjectAtIndex:i];
            erase = 1;
        }
    }
    

    What you are doing essentially will SKIP certain items in the list as you increment your counter after each delete. lets say that you remove object 3, your counter now will update to count 4, but 4 is now what was 5, and 3 is now what was 4, so you essentially are skipping item 4 in this case.

    Try This:

    int erase = 0;
    int i = 0;
    while(i < favs.count){
        if ([favs objectAtIndex:i] == parent){
            [favs removeObjectAtIndex:i];
            erase = 1;
        }else{
            i++;
        }
    }
    

    This will only upgrade the counter if you have NOT deleted the current item.

    [ORIGINAL]
    https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

    Look at the removeObjectAtIndex method notes.

    Direct from Apple:

    “To fill the gap, all elements beyond index are moved by subtracting 1 from their index.”

    This should be being done automatically within Apple’s API. You should not have to do anything, and there really shouldnt be any “holes” left.

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

Sidebar

Related Questions

I have a method in c++ that gets called from python and needs to
I have this method in an exterior class that gets called whenever the charachter
I have this method which gets called from a different class: -(void)artworkInfo:(NSNumber *)pos{ [image
I have a method addClicked that gets called when a button on the navigation
I have this method that logs errors from the Silverlight client: [MethodImpl(MethodImplOptions.NoInlining)] public static
I have this gesture swipe method that I want to call from another method
I have a method that gets called any time a control on my view
I have a method that gets data from the data manager. It looks something
Hey guys. I have a method that gets called each second which I want
I have a method that gets called when entering a chart page in my

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.