I. Basic concepts of a set The NSSet class is provided in the Foundation framework. It is a set of Single-value objects and the elements in the NSSet instance are unordered. Only one object can be saved, It is also divided into variable and immutable set objects (variable set object, NSMutableSet) 2. immutable set-NSSet 1: initialization (like creating an array) // Create a set directly similar to Array Construction NSSet *set1=[[NSSet alloc]initWithObjects:@"one",@"tow", nil]; NSLog(@"%@",set1); 2: Build a set through Arrays // Construct NSArray * array1 = [NSArray arrayWithObjects: @ "one", @ "tow", nil] Through arrays; NSSet * set2 = [NSSet setWithArray: array1]; NSLog (@ "% @", set2 ); 3: build with an existing set // Build with an existing set NSSet *set3=[NSSet setWithSet:set2]; NSLog(@"%@",set3); 3: Number of collection objects// Common methods in Collection NSInteger *count=[set3 count]; NSLog(@"%ld",count); 4: return all elements in the set.// All elements in the Set NSArray *array2 =[set3 allObjects]; NSLog(@"%@",array2); 5: return any element in the set.// Returns any element in the set. NSString *str=[set3 anyObject]; NSLog(@"%@",str); 6: Check whether an element is contained in the collection.// Query whether an element Boolean result1 = [set3 containsObject: @ "two"]; if (result1) {NSLog (@ "contains two ");} else {NSLog (@ "two not included ");} 7: Check whether there is an intersection between a set and a set.// Query whether there is an intersection between Sets BOOL result2= [set1 intersectsSet:set2]; NSLog(@"%d",result2); 8: Set matching// Determine whether the set matches BOOL result3=[set1 isEqualToSet:set2]; NSLog(@"%d",result3); 9: whether it is a subset of a set// Whether it is a subset of a set BOOL result4=[set1 isSubsetOfSet:set2]; NSLog(@"%d",result4); 10: Add a new element to a set to return a new set. NSSet *set5=[NSSet setWithObjects:@"one",nil]; NSSet *appSet=[set5 setByAddingObject:@"tow"]; NSLog(@"%@",appSet); 11: Add a set to a set and return a new set.// Add a set to a set NSSet *set6=[NSSet setWithObjects:@"1",@"2", nil]; NSSet *appSet1=[set5 setByAddingObjectsFromSet:set6]; NSLog(@"%@",appSet1); 12: add an array to a set and return a new set.// Add a number to a set NSArray *appArray=[NSArray arrayWithObjects:@"x",@"y", nil]; NSSet *appSet2=[set5 setByAddingObjectsFromArray:appArray]; NSLog(@"%@",appSet2); Iii. Variable set-NSMutableSet1: Create an initialized variable set // Create an initialized variable set NSMutableSet * mutableSet1 = [NSMutableSet Set]; // empty Set NSMutableSet * mutableSet2 = [NSMutableSet setWithObjects: @ "1", @ "2", nil]; NSMutableSet * mutableSet3 = [NSMutableSet setWithObjects: @ "a", @ "2", nil]; 2: remove the same element from the set// Remove the same part from the two sets [mutableSet2 minusSet:mutableSet3]; NSLog(@"%@",mutableSet2); 3: Calculate the public elements of the two sets.// Obtain the same elements of the two sets. [mutableSet2 intersectSet:mutableSet3]; NSLog(@"%@",mutableSet2); 4: merge two sets// Merge the two sets. [mutableSet2 unionSet:mutableSet3]; NSLog(@"%@",mutableSet2); |