简体   繁体   English

NSArray +从数组中删除项目

[英]NSArray + remove item from array

如何从NSArray中删除项目。

NSArray is not mutable, that is, you cannot modify it. NSArray不可变,也就是说,你不能修改它。 You should take a look at NSMutableArray . 你应该看看NSMutableArray Check out the "Removing Objects" section, you'll find there many functions that allow you to remove items: 查看“删除对象”部分,您会发现有许多功能可以删除项目:

[anArray removeObjectAtIndex: index];
[anArray removeObject: item];
[anArray removeLastObject];
NSMutableArray *arrayThatYouCanRemoveObjects = [NSMutableArray arrayWithArray:your_array];

[arrayThatYouCanRemoveObjects removeObjectAtIndex:your_object_index];

[your_array release];

 your_array = [[NSArray arrayWithArray: arrayThatYouCanRemoveObjects] retain];

that's about it 这就是它

if you dont own your_array(ie it's autoreleased) remove the release & retain messages 如果您不拥有your_array(即它已自动释放),请删除发布并保留消息

This category may be to your taste. 此类别可能符合您的口味。 But! 但! Be frugal with its usage; 使用它是节俭的; since we are converting to a NSMutableArray and back again, it's not at all efficient. 因为我们正在转换为NSMutableArray并再次返回,所以效率并不高。

@implementation NSArray (mxcl)

- (NSArray *)arrayByRemovingObject:(id)obj
{
    if (!obj) return [self copy]; // copy because all array* methods return new arrays
    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:self];
    [mutableArray removeObject:obj];
    return [NSArray arrayWithArray:mutableArray];
}

@end

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

Made a category like mxcl, but this is slightly faster. 制作了类似mxcl的类别,但速度稍快。

My testing shows ~15% improvement (I could be wrong, feel free to compare the two yourself). 我的测试显示了大约15%的改进(我可能错了,可以自己比较两者)。

Basically I take the portion of the array thats in front of the object and the portion behind and combine them. 基本上我将数组的一部分放在对象前面和后面的部分并将它们组合起来。 Thus excluding the element. 因此排除元素。

- (NSArray *)prefix_arrayByRemovingObject:(id)object 
{
    if (!object) {
        return self;
    }

    NSUInteger indexOfObject = [self indexOfObject:object];
    NSArray *firstSubArray = [self subarrayWithRange:NSMakeRange(0, indexOfObject)];
    NSArray *secondSubArray = [self subarrayWithRange:NSMakeRange(indexOfObject + 1, self.count - indexOfObject - 1)];
    NSArray *newArray = [firstSubArray arrayByAddingObjectsFromArray:secondSubArray];

    return newArray;
}

Remove Object from NSArray with this Method: 使用此方法从NSArray删除Object

-(NSArray *) removeObjectFromArray:(NSArray *) array withIndex:(NSInteger) index {
    NSMutableArray *modifyableArray = [[NSMutableArray alloc] initWithArray:array];
    [modifyableArray removeObjectAtIndex:index];
    return [[NSArray alloc] initWithArray:modifyableArray];
}

As others suggested, NSMutableArray has methods to do so but sometimes you are forced to use NSArray, I'd use: 正如其他人所说,NSMutableArray有方法可以这样做,但有时你被迫使用NSArray,我会使用:

NSArray* newArray = [oldArray subarrayWithRange:NSMakeRange(1, [oldArray count] - 1)];

This way, the oldArray stays as it was but a newArray will be created with the first item removed. 这样,oldArray保持不变,但是将删除第一个项目创建newArray。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM