#import <Foundation/Foundation.h>
//create an integer object
#define INTOBJ(v) [NSNumber numberWithInteger: v]
// add a print method to NSSet with the printing catagory
@interface NSSet (Printing);
-(void) print;
@end
@implementation NSSet (Printing);
-(void) print
{
printf("{"); // NOT NSLOG because we need to print on same line
for (NSNumber *element in self)
printf (" %li ", (long) [element integerValue]);
printf("}\n");
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableSet *set1 = [NSMutableSet setWithObjects: INTOBJ(1),
INTOBJ(3), INTOBJ(5), INTOBJ(10), nil];
NSMutableSet *set2 = [NSMutableSet setWithObjects: INTOBJ(-5),
INTOBJ(100), INTOBJ(3), INTOBJ(5), nil];
NSMutableSet *set3 = [NSMutableSet setWithObjects: INTOBJ(12),
INTOBJ(200), INTOBJ(3), nil];
NSLog(@"set1: ");
[set1 print];
NSLog(@"set2: ");
[set2 print];
// equalty test
if ([set1 isEqualToSet: set2] == YES)
NSLog(@"set1 equals set2");
else
NSLog(@"set1 does not equal set2");
// membership test
if ([set1 containsObject: INTOBJ(10)] == YES)
NSLog(@"set1 contains 10");
else
NSLog(@"set1 does not contain 10");
if ([set2 containsObject:INTOBJ(10)] == YES)
NSLog(@"set2 contains 10");
else
NSLog(@"set2 does not contain 10");
// add and remove objects from mutable set set1
[set1 addObject: INTOBJ(4)];
[set1 removeObject:INTOBJ(10)];
NSLog(@"set1 after adding 4 and removing 10: ");
[set1 print];
//get intersection of two sets
[set1 intersectSet: set2];
NSLog(@"set 1 intersect set2: ");
[set1 print];
//union of two sets
[set1 unionSet: set3];
NSLog(@"set1 union set3: ");
[set1 print];
[pool drain];
return 0;
}
[/pre]