Есть ли способ проверить, пуст ли набор?
NSMutableSet *setEmpty = [[NSMutableSet alloc] init]; // Code to do things... // Check for empty set? [setEmpty release];
* 1004 Гэри *
Вы можете использовать [setEmpty count], чтобы увидеть, сколько элементов в наборе ... так:
if ([setEmpty count] == 0) {
или
if (![setEmpty count]) {
и т.д ...
Я не видел явного метода 'isEmpty' в http://developer.apple.com/mac/library/documentation/cocoa/Reference/Foundation/Classes/NSSet_Class/Reference/Reference.html, но если он существует, воспользуйтесь этим вместо проверки счетчика.
Как насчет этого для действительно больших сетов? Цель состоит в том, чтобы не делать подсчет каждый раз.
NSSet *mySet = ... if ([mySet anyObject] == nil) { // The set is empty }
… Вот наиболее часто используемый файл в моем общем хранилище: DMCommonMacros.h static inline BOOL IsEmpty(id thing) { return thing == nil || ([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) || ([thing respondsToSelector:@selector(count)] && [(NSArray *)thing count] == 0); }
static inline BOOL IsEmpty(id thing) { return thing == nil || ([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) || ([thing respondsToSelector:@selector(count)] && [(NSArray *)thing count] == 0); }
- Wil Shiply
Эта функция будет работать на всех контейнерах Какао, строках и NSData.