objective-ccocoanssetnsmutablesetnscountedset

How to set object count to zero(or some value) in NSCountedSet?


I have following NSCountedSet

<NSCountedSet: 0x53dfc0> (item1 [2000], item2 [9000], item3 [200], item4  [3000])

Now i want to remove item1 object from my set.
one solution is

while([mySet countForObject:item1])
 [mySet removeObject:item1];

Output:

<NSCountedSet: 0x53dfc0> (item2 [9000], item3 [200], item4  [3000])

or i want to remove only 1000 item1 object from my set.

    NSUInteger count = [mySet countForObject:item1];
    while(count)
    {
     [mySet removeObject:item1];
     --count;
    }

Output:

<NSCountedSet: 0x53dfc0> (item1 [1000], item2 [9000], item3 [200], item4  [3000])

is there any better solution for this ?


Solution

  • You could filter with a predicate

    [mySet filterUsingPredicate:[NSPredicate predicateWithFormat:@"self != %@", item1]];