NSArray + remove item from array

后端 未结 7 1787
-上瘾入骨i
-上瘾入骨i 2020-12-15 03:29

How to remove an item from NSArray.

7条回答
  •  星月不相逢
    2020-12-15 03:53

    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.

    @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
    

提交回复
热议问题