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

510 views Asked by At

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 ?

3

There are 3 answers

2
Paul.s On BEST ANSWER

You could filter with a predicate

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

NSCountedSet does not offer methods to modify the count of a certain object by a certain amount (a pity really).

If speed is not an issue you can used the methods you described above. However if you need something else: subclassing NSMutableSet to provide this "counting" functionality is probably your best option.

4
WhiteTiger On

I do not think you understand the problem, so I get these results:

<NSCountedSet: 0x6d9ee40> (two [1], three [1], one [1], four [1])

[mySet removeObject:@"one"];

<NSCountedSet: 0x6d9ee40> (two [1], three [1], four [1])