2

is it possible to remove multiple objects from an NSMutableArray by passing it a list or an array of objects you want to remove? Right now I have a deleteAll method that deletes all messages from a Parse server and then removes the messages from the local array.

However as this is asynchronous by the time it finishes deleting and then calls[myArray removeAllObjects] there may be a new message received that I don't want to accidentally delete from the array.

So I am thinking of copying the message array before I do the delete operation and when the asynchronous call is complete remove all elements from the message array that are the same as the copy.

Is there a nice way to do it or is my only option to iterate the array and delete if it matches what is in the copy one?

2 Answers 2

6

Look at the - (void)removeObjectsInArray:(NSArray *)otherArray method of NSMutableArray

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for this, just what I was looking for. Don't know why I didn't notice it in the docs
0

Can't you just use a for loop and delete the objects you want? like so:

NSMutableArray *yourArray = [..... etc....];
NSMutableArray *editArray = [[NSMutableArray alloc] init];
editArray = [yourArray copy];

NSMutableArray *itemsToDelete = [....items....];

for (int loop = 0; loop < [yourArray count]; loop++) {

    for (int loop_2 = 0; loop_2 < [editArray count]; loop_2++) {

        if (editArray[loop_2] == yourArray[loop]) {
            [editArray removeObjectAtIndex:loop_2];
        }
    }
}

1 Comment

Could use this yes, but was just looking for a more efficient method. Thanks though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.