我有以下NSCountedSet
<NSCountedSet: 0x53dfc0> (item1 [2000], item2 [9000], item3 [200], item4 [3000])
现在我想从我的集合中删除 item1
对象。
一种解决方案是
while([mySet countForObject:item1])
[mySet removeObject:item1];
输出:
<NSCountedSet: 0x53dfc0> (item2 [9000], item3 [200], item4 [3000])
或者我只想从我的集合中删除 1000 个 item1
对象。
NSUInteger count = [mySet countForObject:item1];
while(count)
{
[mySet removeObject:item1];
--count;
}
输出:
<NSCountedSet: 0x53dfc0> (item1 [1000], item2 [9000], item3 [200], item4 [3000])
有没有更好的解决方案?
最佳答案
您可以使用谓词进行过滤
[mySet filterUsingPredicate:[NSPredicate predicateWithFormat:@"self != %@", item1]];
关于objective-c - 如何在 NSCountedSet 中将对象计数设置为零(或某个值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10785126/