直接上代码:
NSSet *set1 = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil] ;
NSLog( @"%@", set1 ) ;
NSSet *set2 = [NSSet setWithObjects:@"12", @"23", @"34", nil] ;
NSLog( @"%@", set2 ) ;
NSArray *array = @[@1, @2, @2] ;
NSSet *set3 = [[NSSet alloc] initWithArray:array] ;
NSLog( @"%@", set3 ) ;
NSSet *set4 = [NSSet setWithArray:array] ;
NSLog( @"%@", set4 ) ;
NSLog( @"%ld", [set4 count] ) ;
id object1 = [set4 anyObject] ;
NSLog( @"%@", object1 ) ;
NSString *result1 = [set4 containsObject:@2] ? @"YES" : @"NO" ;
NSLog( @"%@ is contained int set %@", @2, result1 ) ;
NSMutableSet *mutableSet1 = [[NSMutableSet alloc] init] ;
NSLog( @"%@", mutableSet1 ) ;
NSMutableSet *mutableSet2 = [NSMutableSet set] ;
NSLog( @"%@", mutableSet2 ) ;
NSMutableSet *mutableSet3 = [[NSMutableSet alloc] initWithSet:set1] ;
NSLog( @"%@", mutableSet3 ) ;
NSMutableSet *mutableSet4 = [NSMutableSet setWithSet: set1] ;
NSLog( @"%@", mutableSet4 ) ;
[mutableSet4 addObject:@4] ;
NSLog( @"%@", mutableSet4 ) ;
[mutableSet4 removeObject:@4] ;
NSLog( @"%@", mutableSet4 ) ;
[mutableSet4 removeAllObjects] ;
NSLog( @"%@", mutableSet4 ) ;
NSCountedSet *countSet1 = [NSCountedSet set] ;
[countSet1 addObject:@1] ;
[countSet1 addObject:@2] ;
[countSet1 addObject:@3] ;
[countSet1 addObject:@2] ;
NSLog( @"%@", countSet1 ) ;
NSLog( @"%ld", [countSet1 countForObject:@5] ) ;