ios 从 NSArray 中删除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7426570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:32:06  来源:igfitidea点击:

remove object from NSArray

iosobjective-cuitableviewnsmutablearraynsarray

提问by Matt

How can I remove an object from a reversed NSArray.

如何从反转的NSArray.

Currently I have a NSMutableArray, then I reverse it with

目前我有一个NSMutableArray,然后我用

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

now I need to remove at item from reversedCalEventsor calEventsand automatically refresh the table the array is displayed in based on conditions. i.e.

现在我需要从reversedCalEvents或 中删除 at itemcalEvents并根据条件自动刷新显示数组的表。IE

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

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

我怎样才能做到这一点?我无法让它工作。

回答by Mark Adams

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

您将需要一个可变数组才能删除对象。尝试reversedCalEvents使用mutableCopy.

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

if (someInt == someOtherInt)
{
    [reversedCalEvents removeObject:object];
}

回答by Rudolf Adamkovi?

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

回答by alloc_iNit

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

NSArray 不可编辑,因此您无法修改它。

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

您可以将该数组复制到 NSMutableArray 并从中删除对象。最后将 NSMutableArray 的值重新分配给您的 NSArray。

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

从这里你会得到一个更好的主意...... NSArray + 从数组中删除项目

回答by Beltalowda

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

首先,您应该阅读NSMutableArray类本身以熟悉它。

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

其次,这个问题应该向您展示一种从 NSMutableArray 实例中删除对象的简单方法。

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

第三,您可以通过向 UITableView 发送reloadData消息来刷新它。

回答by sourabh sharotria

you can try this:-

你可以试试这个:-

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