开发者

remove object from NSArray

开发者 https://www.devze.com 2023-04-05 04:33 出处:网络
How can I remove an object from a reversed NSArray. Currently I have a NSMutableArray, then I reverse it with

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.


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


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


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


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.


you can try this:-

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

精彩评论

暂无评论...
验证码 换一张
取 消