The collection class in iOS is like a collection in mathematics, where elements must be unique, storage elements are unordered, and storage elements must be object types.
The set is divided into immutable sets (Nsset) and mutable sets (Nsmutableset)
Immutable collection (Nsset)
To create a collection object
1 1Nsset *Set= [Nsset setwithobjects:@"a",@"P",@"P", nil];2 2NSLog (@"set:%@",Set);3 //gets the number of elements in the collection4 3NSLog (@"Count:%ld",Set. Count);5 //gets an element in the collection6 4NSLog (@"object:%@", [SetAnyobject]);
Because the total element of the collection is unique, the count value printed at this point is 2.
Determines whether an object is contained in the collection
1 if ([Set containsobject:@ "a"]) {2 NSLog ( " contains the object in the collection "); 3 Else {4 NSLog ( " The object is not included in the collection "); 5 }
Variable set (nsmutableset)
To create a collection object
1Nsmutableset *mset = [NsmutablesetSet];2 //adding elements3[MSet AddObject:@"I"];4[MSet AddObject:@"O"];5[MSet AddObject:@"S"];6NSLog (@"MSet:%@", MSet);
Delete Element
1 // Delete 2 [mSet removeobject:@ "i"]; 3 NSLog (@ "mSet:%@", MSet);
Count Set (Nscountedset)
Nscountedset is a subclass of Nsmutableset that can record the number of repetitions of an element, adding a count function based on set.
1 //Create a collection2Nscountedset *cset = [NscountedsetSet];3 //adding elements4[CSet AddObject:@"a"];5[CSet AddObject:@"P"];6[CSet AddObject:@"P"];7NSLog (@"CSet:%@", CSet);8NSLog (@"Count:%ld", cset.count);9 //the number of times the element was added in the collectionTenNSLog (@"Count:%ld", [CSet Countforobject:@"P"]);
Collection classes in Objective-c