Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove object from NSArray

How can I remove an object from a reversed NSArray.

Currently I have a NSMutableArray, then I reverse it with

NSArray* reversedCalEvents = [[calEvents reverseObjectEnumerator] allObjects];

now I need to remove at item from reversedCalEvents or calEvents and automatically refresh the table the array is displayed in based on conditions. i.e.

if(someInt == someOtherInt){
    remove object at index 0
}

How can I do this? I cannot get it to work.

like image 713
Matt Avatar asked Nov 27 '22 22:11

Matt


2 Answers

Here's a more functional approach using Key-Value Coding:

@implementation NSArray (Additions)

- (instancetype)arrayByRemovingObject:(id)object {
    return [self filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF != %@", object]];
}

@end
like image 166
Rudolf Adamkovič Avatar answered Dec 16 '22 22:12

Rudolf Adamkovič


You will need a mutable array in order to remove an object. Try creating reversedCalEvents with mutableCopy.

NSMutableArray *reversedCalEvents = [[calEvents reverseObjectEnumerator] allObjects] mutableCopy];

if (someInt == someOtherInt)
{
    [reversedCalEvents removeObject:object];
}
like image 23
Mark Adams Avatar answered Dec 16 '22 23:12

Mark Adams