I want to insert a bunch of objects into a NSMutableArray. Then I would remove them one by one when the time fits. Every inserted objects must be removed.
我想将一堆对象插入到NSMutableArray中。然后我会在适合的时候逐个删除它们。必须删除每个插入的对象。
However, if I have several copies of the same object, I just want to remove one of them.
但是,如果我有同一个对象的多个副本,我只想删除其中一个。
How would I do that?
我该怎么办?
5 个解决方案
#1
3
NSMutableArray *arr = [@[@1, @1, @5, @6, @5] mutableCopy]; // a copy of your array
NSMutableSet *removedObjects = [NSMutableSet setWithArray:arr];
for (id obj in removedObjects) {
[arr removeObjectAtIndex:[arr indexOfObject:obj]]; // removes the first identical object
}
Also note that if your array is filled with custom objects, you need to implements hash
and isEqual:
so the comparisons can work.
另请注意,如果您的数组中填充了自定义对象,则需要实现hash和isEqual:因此比较可以正常工作。
#2
1
are any of these functions what you are looking for?
您正在寻找的这些功能中的任何一个?
[array removeObjectAtIndex:(NSUInteger)];
[array removeLastObject];
[array removeObject:(id)];
#3
1
[myMutableArray removeObjectAtIndex: 0];
or
[myMutableArray removeLastObject];
To remove the first and last objects respectively.
分别删除第一个和最后一个对象。
#4
1
I don't think it's a good design for [NSMutableArray removeObject:]
to remove all the occurences but we can avoid it by first getting an index using indexOfObject:
and then removing the object using removeObjectAtIndex:
我不认为[NSMutableArray removeObject:]删除所有出现的好设计但我们可以通过首先使用indexOfObject获取索引来避免它:然后使用removeObjectAtIndex删除对象:
#5
0
i prefer using index of object; it will return the index of object first appeared in the array
我更喜欢使用对象索引;它将返回首先出现在数组中的对象索引
NSMutableArray * first = [@[@"2",@"3",@"4",@"5"] mutableCopy];//your first array
NSMutableArray * willBeAppend = [@[@"2",@"3",@"4",@"5"] mutableCopy];//new objects to append
[first addObjectsFromArray:willBeAppend];//append new objects
id objectToBeRemoved = @"3";// object will be removed
NSInteger objIx = [first indexOfObject:objectToBeRemoved];// index of object
if (objIx != NSNotFound) {
[first removeObjectAtIndex:objIx]; //remove object
NSLog(@"%@",first);
}
#1
3
NSMutableArray *arr = [@[@1, @1, @5, @6, @5] mutableCopy]; // a copy of your array
NSMutableSet *removedObjects = [NSMutableSet setWithArray:arr];
for (id obj in removedObjects) {
[arr removeObjectAtIndex:[arr indexOfObject:obj]]; // removes the first identical object
}
Also note that if your array is filled with custom objects, you need to implements hash
and isEqual:
so the comparisons can work.
另请注意,如果您的数组中填充了自定义对象,则需要实现hash和isEqual:因此比较可以正常工作。
#2
1
are any of these functions what you are looking for?
您正在寻找的这些功能中的任何一个?
[array removeObjectAtIndex:(NSUInteger)];
[array removeLastObject];
[array removeObject:(id)];
#3
1
[myMutableArray removeObjectAtIndex: 0];
or
[myMutableArray removeLastObject];
To remove the first and last objects respectively.
分别删除第一个和最后一个对象。
#4
1
I don't think it's a good design for [NSMutableArray removeObject:]
to remove all the occurences but we can avoid it by first getting an index using indexOfObject:
and then removing the object using removeObjectAtIndex:
我不认为[NSMutableArray removeObject:]删除所有出现的好设计但我们可以通过首先使用indexOfObject获取索引来避免它:然后使用removeObjectAtIndex删除对象:
#5
0
i prefer using index of object; it will return the index of object first appeared in the array
我更喜欢使用对象索引;它将返回首先出现在数组中的对象索引
NSMutableArray * first = [@[@"2",@"3",@"4",@"5"] mutableCopy];//your first array
NSMutableArray * willBeAppend = [@[@"2",@"3",@"4",@"5"] mutableCopy];//new objects to append
[first addObjectsFromArray:willBeAppend];//append new objects
id objectToBeRemoved = @"3";// object will be removed
NSInteger objIx = [first indexOfObject:objectToBeRemoved];// index of object
if (objIx != NSNotFound) {
[first removeObjectAtIndex:objIx]; //remove object
NSLog(@"%@",first);
}