1 intMainintargcConst Char*argv[]) {2 @autoreleasepool {3 4 //Create a Collection object Note: If the collection has been written two or more times the same object is printed only to see one5Nsset *set1 = [[Nsset alloc] Initwithobjects:@" One",@" Both",@"three",@"three", nil];6 //Note: The outermost layer inside the set is the curly brace and then the parentheses7 //The collection is unordered it is hashed hash sort "data structure" hash algorithm8NSLog (@"%@", Set1);9 Ten //the number of elements in the collection OneNSLog (@"number of collections:%ld", [Set1 Count]); A - - //whether to include an element theBOOL contains = [Set1 containsobject:@" Both"]; -NSLog (@"contains:%d", contains); - - //whether two collections are equal +Nsset *set2 = [Nsset setwithobjects:@" One",@" Both",@"three",@" Four", nil]; -BOOL isequal =[Set1 Isequaltoset:set2]; + A if(isequal) { atNSLog (@"two sets equal"); -}Else { -NSLog (@"two sets are not equal"); - } - - in //determines whether the first collection is a subset of the second set - toBOOL Issubset =[Set1 Issubsetofset:set2]; +NSLog (@"whether it is a sub-collection:%d", issubset); - the * //enumeration traversal for while Do-while Forin enumerator $ //Collection enumerator that takes set2 often iterates through the collection with enumeratorsPanax NotoginsengNsenumerator *enumerator =[Set2 objectenumerator]; -NSString *str; the //Nextobject Next Element + while(str =[Enumerator Nextobject]) { ANSLog (@"%@", str); the } + - //to create a collection by way of an array (the array turns into a collection) $Nsset *set3 = [Nsset setwitharray:@[@" One",@"Five"]]; $NSLog (@"set3:%@", Set3); - //View collection All elements return an array (collection to group) -Nsarray *array1 =[Set3 allobjects]; theNSLog (@"array1:%@", array1); - Wuyi the /******** variable set *******/ - //Create an empty collection WuNsmutableset *mset =[[Nsmutableset alloc] init]; -NSLog (@"%@", MSet); About $ //Increase - //adding a collection element -[MSet AddObject:@" One"]; -[MSet AddObject:@" One"]; A[MSet AddObject:@" Both"]; +[MSet AddObject:@"three"]; theNSLog (@"%@", MSet); - $ //Delete the //To delete a collection element the[MSet Removeobject:@" One"]; theNSLog (@"%@", MSet); the - in //adding collections to the collection the [MSet Unionset:set2]; theNSLog (@"------%@", MSet); About the //When deleting a collection in a collection , if you want to delete more than one collection element in the collection, Mset does not collapse when this collection element is deleted. the //nsset *_set = [Nsset setwithobjects:@ "one", @ "one", @ "three", @ "four", @ "1", nil]; the + //[MSet Minusset:_set]; - //NSLog (@ "======%@", MSet); the Bayi /******** index collection or index collection **********/ the //Inherit NSObject the //Create an index collection by creating an index collection in the specified range Note: Range do not exceed the range will crash - //nsindexset *set4 = [[Nsindexset alloc] Initwithindexesinrange:nsmakerange (2, 4)]; - the //creates an empty, mutable index collection theNsmutableindexset *set5 =[[Nsmutableindexset alloc] init]; the //add subscript to a mutable index collection the[Set5 Addindex:0]; -[Set5 Addindex:2]; the[Set5 Addindex:3]; the[Set5 Addindex:6]; the 94 theNsarray *allarray = [[Nsarray alloc] Initwithobjects:@" One",@" Both",@"three",@" Four",@"Five",@"Six",@"Seven", nil]; the //get an array from an indexed collection (array elements in range of index range) theNsarray *subarray =[Allarray Objectsatindexes:set5];98NSLog (@"++++++++%@", subarray); About - 101 }102 return 0;103}
Nsset Basic Use