Knowledge Point of NSSet and NSMutableSet in IOS collection, nssetnsmutableset
NSSet is not much different from NSArray in actual application, but if you want to find an element in NSArray, You need to traverse the entire array, which is inefficient. NSSet directly finds the position of a specific element based on the hash algorithm, which is highly efficient. NSSet is an unordered collection class that manages objects. The biggest feature of NSSet is that repeated objects are not allowed in the collection, which has the same meaning as the mathematical set. Except for unordered data, repeatable is not allowed. Other functions are the same as NSArray. Note that only cocoa objects can be added to NSSet and NSArray. If you need to add basic data types (int, float, BOOL, double, etc.), data needs to be encapsulated into the NSNumber type. This is mainly for some common operations. For some other operations, see the relevant documentation. The following code also uses a third-party plug-in BlocksKit;
1: Some Common NSSet operations
NSSet * newSet = [NSSet setWithObjects: @ "wujy", @ "cnblogs", @ "age", nil]; NSLog (@ "number of sets: % ld ", [newSet count]); // it will not be input in the order added above, so NSSET has no sequence NSEnumerator * enumeratorset = [newSet objectEnumerator]; for (NSObject * obj in enumeratorset) {NSLog (@ "key: % @", obj);} // whether BOOL isExict = [newSet containsObject: @ "wujy"]; NSLog (@ "whether it exists: % d ", isExict); // returns any element NSString * anyObje = [newSet anyObject]; NSLog (@" returns any element: % @ ", AnyObje); // determines whether the element is contained and returns NSObject * memberObj = [newSet member: @" age "]; if (memberObj) {NSLog (@ "existing element (age): % @", memberObj);} // create nsset NSSet * twoSet = [[nsset alloc] initWithArray: @ [@ 2, @ 10, @ 12, @ "wujy"]; // determines whether two nssets are equal. BOOL isEaual = [newSet is toset: twoSet]; NSLog (@ "determine whether two nssets are equal (0): % d", isEaual); // determine whether two nssets overlap BOOL isInterSects = [newSet intersectsSet: twoSet]; NSLog (@ "determine whether two nssets overlap (1): % d", isI NterSects); // Blocks NSSet * blockSet = [[NSSet alloc] initWithObjects: @ 1, @ 2, @ 3, @ 4, @ 5, @ 6, nil]; // traverse [blockSet bk_each: ^ (id obj) {NSLog (@ "the value of block traversal is: % @", obj);}]; // filter NSSet * selectSet = [blockSet bk_select: ^ BOOL (id obj) {BOOL select = [obj intValue]> 3? YES: NO; return select;}]; NSLog (@ "filtered nsset (6, 5, 4): % @", selectSet ); // filter NSSet * matchSet = [blockSet bk_match: ^ BOOL (id obj) {BOOL select = [obj intValue] <4? YES: NO; return select;}]; NSLog (@ "matchSet filtered nsset (3): % @", matchSet ); // obtain the inverse filter NSSet * rejectSet = [blockSet bk_reject: ^ BOOL (id obj) {BOOL select = [obj intValue] <4? YES: NO; return select;}]; NSLog (@ "nsset (6, 5, 4) after anti-filtering: % @", rejectSet ); // process each item NSSet * mapSet = [blockSet bk_map: ^ id (id obj) {return @ ([obj intValue] + 1);}]; NSLog (@ "the modified value is (7, 3, 6, 2, 5, 4): % @", mapSet); // if the condition is met, return bool BOOL isSelected = [blockSet bk_any: ^ BOOL (id obj) {BOOL select = [obj intValue]> 3? YES: NO; return select;}]; NSLog (@ "% d meets condition 1", isSelected ); // determine if all items meet this condition BOOL allSelected = [blockSet bk_all: ^ BOOL (id obj) {BOOL select = [obj intValue]> 2? YES: NO; return select;}]; NSLog (@ "% d meets condition 0", allSelected ); // determine if all items do not meet this condition. BOOL noneSelected = [blockSet bk_none: ^ BOOL (id obj) {BOOL select = [obj intValue]> 50? YES: NO; return select;}]; NSLog (@ "% d meets condition 1", noneSelected );
2: Some Common NSMutableSet operations
// Create NSMutableSet * mutableSet = [[NSMutableSet alloc] initWithCapacity: 5]; [mutableSet addObject: @ 1]; [mutableSet addObjectsFromArray: @ [@ 2, @ 3, @ 4]; NSLog (@ "added NSMutableSet (,): % @", mutableSet); // Add nsset to [mutableSet unionSet: [NSSet setWithObjects: @ 5, @ 6, nil]; NSLog (@ "added NSMutableSet (,): % @", mutableSet ); // remove the element [mutableSet removeObject: @ 5]; NSLog (@ "removed element (, 4): % @", mutableSet );/ /Delete the [mutableSet minusSet: [nsset setWithObjects: @ 1, @ 2, nil]; NSLog (@ "removed NSSet elements (, 4 ): % @ ", mutableSet); // nsset is converted to nsmutableset NSSet * newSet = [NSSet setWithObjects: @ 10, @ 11, @ 12, nil]; NSMutableSet * newMutableSet = [NSMutableSet set]; [newMutableSet setSet: newSet]; NSLog (@ "the converted mutableset value is (12, 10, 11): % @", newMutableSet ); // Blocks // filter NSMutableSet * blockMutableSet = [[NSMutableSet alloc] initWithObj Ects: @ 20, @ 23, @ 25, nil]; [blockMutableSet bk_effecmselect: ^ BOOL (id obj) {BOOL select = [obj intValue]> 22? YES: NO; return select;}]; NSLog (@ "filtered nsmutableset (25, 23): % @", blockMutableSet); // obtain the anti-filter [blockMutableSet bk_effecmreject: ^ BOOL (id obj) {BOOL select = [obj intValue]> 24? YES: NO; return select;}]; NSLog (@ "nsmutableset (23): % @", blockMutableSet ); // modify the item [blockMutableSet bk_effecmmap: ^ id (id obj) {return @ ([obj intValue] + 1);}]; NSLog (@ "modified nsmutableset (24): % @", blockMutableSet );
3: NSIndexSet
// Index set NSIndexSet * indexSet = [[NSIndexSet alloc] initWithIndexesInRange: NSMakeRange (1, 3)]; // The number in the set is 123 // extract the element NSArray * array = [[NSArray alloc] initWithObjects: @ "one", @ "two ", @ "three", @ "four", nil]; NSArray * newArray = [array objectsAtIndexes: indexSet]; // return @ "two", @ "three ", @ "four" // variable index set NSMutableIndexSetNSMutableIndexSet * indexSet = [[NSMutableIndexSet alloc] init]; [indexSet addIndex: 0] [indexSet addIndex: 3]; [indexSet addIndex: 5]; // obtain the specified NSArray * array = [[NSArray alloc] initWithObjects: @ "one", @ "two", @ "three ", @ "four", @ "five", @ "six", nil]; NSArray * newArray = [array objectsAtIndexes: indexSet]; // return @ "one ", @ "four", @ "six"