5

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.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Matt
  • 2,920
  • 6
  • 23
  • 33

5 Answers5

13

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
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
12

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];
}
Mark Adams
  • 30,776
  • 11
  • 77
  • 77
2

NSArray is not editable, so that you cannot modify it.

You can copy that array to NSMutableArray and remove objects from it. And finally reassign the values of the NSMutableArray to your NSArray.

From here you will get a better idea... NSArray + remove item from array

Community
  • 1
  • 1
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
0

you can try this:-

NSMutableArray* reversedCalEvents = [[[calEvents reverseObjectEnumerator] allObjects] mutableCopy];
            [reversedCalEvents removeLastObject];
Kundan
  • 3,084
  • 2
  • 28
  • 65
0

First you should read up on the NSMutableArray class itself to familiarize yourself with it.

Second, this question should show you an easy way to remove the objects from your NSMutableArray instance.

Third, you can cause the UITableView to refresh by sending it the reloadData message.

Community
  • 1
  • 1
Beltalowda
  • 4,558
  • 2
  • 25
  • 33