简体   繁体   中英

removing Objects from NSMutableArrays

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];
}

}

When you remove an object from a mutable array, it doesn't "leave a hole". Every other object in the mutable array just moves one notch down and the count (or length) of the array shrinks by the number of objects you've removed.

If you're seeing "holes" in your User Interface, you may be having trouble with your table view refresh methods or however else you're displaying the contents of the array. But the base mutable array class definitely knows how to deal with removing and adding and managing its own objects.

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

Removing an object from an NSMutableArray does not leave holes. Apple's array implementation is not a sparse one, meaning the array is resized when you remove an object so that there are no blank spaces.

In addition to @trumpetlicks answer, you can also try this:
Edit: shorter version.

if ([favs containsObject:parent])  //check for present of parent in the favs
{
    [favs removeObjectIdenticalTo:parent];  //remove all occurrences of parent
} else
    [favs addObject:parent];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM