简体   繁体   中英

Removing multiple objects from an NSMutableArray

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?

查看NSMutableArray- (void)removeObjectsInArray:(NSArray *)otherArray方法

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

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