tags:

views:

60

answers:

2

Is there a way to check if a set is empty?

NSMutableSet *setEmpty = [[NSMutableSet alloc] init];

// Code to do things...

// Check for empty set?

[setEmpty release];

gary

+4  A: 

You can use [setEmpty count] to see how many elements are in the set... so:

if ([setEmpty count] == 0) {

or

if (![setEmpty count]) {

etc...

I didn't see an explicit 'isEmpty' method on http://developer.apple.com/mac/library/documentation/cocoa/Reference/Foundation/Classes/NSSet_Class/Reference/Reference.html but if it exists, go for that instead of checking the count.

Malaxeur
Thank you Malaxeur, perfect I looked for that both in the docs and in the Xcode auto-complete and missed it both times :( I don't think there is an 'isEmpty' as I got a missing method error when testing it. Anyways count is perfect, thank you for the answer and your time.
fuzzygoat
If you use it a lot, you could add an `isEmpty` method as a category on `NSSet`, which a body like `- (BOOL)isEmpty { return [self count] == 0; }`.
mipadi
+1  A: 
Vincent Gable