NSArray + remove item from array

后端 未结 7 1772
-上瘾入骨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
    
    0 讨论(0)
提交回复
热议问题