on NSSet

Last-modified: 2014-01-08 (水) 16:02:49

#import

#pragma
@interface NSSet (myCategory)

  • (void)myDescription;
    @end

@implementation NSSet (myCategory)

  • (void)myDescription {
       for (id anObject in self) {
           NSLog(@"%@", [anObject description]);
       }
    }

@end

#pragma
int main(int argc, const char * argv[])
{

   // NSArray *anArrayIntersects =  @[@(0),@(1),@(3),@(4),@(5),@(6),@(7),@(8),@(9)];
   NSArray *anArray = @[@"拾", @"イレブン", @"twenteen", @"ゴルゴ", @"焼酎", @"いちご",];
   // NSArray *anArrayB = @[@"123", @"234", @"567", @"890", @"", @"012"];

#pragma

   // MARK: 不変の NSSet は、余り使い道がないようです。重複要素を取り除いたりできるだけかな。

#pragma

   @autoreleasepool {

#pragma

       @autoreleasepool {
           NSSet *aSet1 = [[NSSet alloc] initWithArray:anArray];
           NSLog(@"initWithArray: %@", aSet1);
       }

#pragma

       NSSet *aSet = [NSSet setWithObjects:@(0),@(1),@(3),@(4),@(5),@(6),@(7),@(8),@(9), @"拾", @"イレブン", @"twenteen", @"ゴルゴ", @"焼酎", @"いちご", nil];
       NSLog(@"Original NSSet: %@", aSet);

#pragma

       NSLog(@"count %lu", (unsigned long)[aSet count]);

#pragma

       NSLog(@"get components as an array\n%@", [aSet allObjects]);

#pragma

#pragma

       @autoreleasepool {
           BOOL doesContain;
           NSString *aStr;
           aStr = @"ゴルゴ";
           doesContain = [aSet containsObject:aStr];
           NSLog(@"aSet contains '%@'? %@", aStr, doesContain ? @"Yes!" : @"NO.");
           aStr = @"012";
           doesContain = [aSet containsObject:aStr];
           NSLog(@"aSet contains '%@'? %@", aStr, doesContain ? @"Yes!" : @"NO.");
       }

#pragma

       @autoreleasepool {
           NSSet *bSet = [NSSet setWithSet:aSet];
           BOOL isEqual = [aSet isEqual:bSet];
           NSLog(@"is equlity? %@", isEqual ? @"Yes." : @"No.");
       }

#pragma

       @autoreleasepool {
           NSSet *aSuperSet = [NSSet setWithObjects:@"123", @"234", @"567", @"890", @"43", @"012", @(0),@(1),@(3),@(4),@(5),@(6),@(7),@(8),@(9), @"拾", @"イレブン", @"twenteen", @"ゴルゴ", @"焼酎", @"いちご", nil];
           BOOL isSubset = NO;
           isSubset = [aSet isSubsetOfSet:aSuperSet];
           NSLog(@"is subset? %@", isSubset ? @"Yes." : @"No.");
       }

#pragma

#pragma

       @autoreleasepool {
           NSLog(@"enumerator を使った(従来型)列挙");
           NSEnumerator *anEnumerator = [aSet objectEnumerator];
           id anObject;
           while ((anObject = [anEnumerator nextObject])) {
               NSLog(@"  a member: %@", [anObject description]);
           }
       }
   }

#pragma

   @autoreleasepool {

#pragma

       NSMutableSet *aMutableSet = [[NSMutableSet alloc] initWithCapacity:15];

#pragma

       [aMutableSet addObject:@"124"];
       [aMutableSet addObject:@"あかさ"];
       [aMutableSet addObject:@"234"];
       [aMutableSet addObject:@"あかさ"];
       [aMutableSet addObject:@"234"];
       [aMutableSet addObject:@"かきく"];
       NSLog(@"mutable set added an object: \%@", aMutableSet);

#pragma

       [aMutableSet removeObject:@"あかさ"];
       NSLog(@"set removed object: \%@", aMutableSet);

#pragma

       NSSet *otherSet = [NSSet setWithObjects:@"124", @"234", @"987", @"876", @"和集合", @"union!", nil];
       [aMutableSet unionSet:otherSet];
       NSLog(@"a mutable set unioned with other set: %@", aMutableSet);

#pragma

       NSLog(@"共通要素を取り除く minusSet");
       NSSet *myMinusSet = [NSSet setWithObjects:@"124", @"union!", nil];
       [aMutableSet minusSet:myMinusSet];
       [aMutableSet myDescription];

#pragma

       @autoreleasepool {
           NSLog(@"通部分(積集合)だけを残す, intersectSet:");
           NSSet *otherSet = [NSSet setWithObjects:@"124", @"234", @"987", @"積集合のみ", nil];
           [aMutableSet intersectSet:otherSet];
           [aMutableSet myDescription];
       }
   }
   return 0;

}