iOS collection Nsset and nsmutableset knowledge points

Source: Internet
Author: User

Nsset does not differ much from Nsarray in practical applications, but if you want to find an element in Nsarray, you need to traverse the entire array and be inefficient. Nsset, when looking for a particular element, finds the location of the element directly based on the hash algorithm, and is highly efficient. Nsset is an unordered, managed collection class for objects, with the greatest feature being that duplicate objects are not allowed in the collection, and that the mathematical set meanings are the same. In addition to disorder, do not repeat, the other functions and Nsarray are the same; It should be noted that Nsset,nsarray can only add cocoa objects, if you need to join the basic data type (int,float,bool,double, etc.), The data needs to be encapsulated into a nsnumber type. The main is some common operation, do not see its corresponding documentation, the following code part of the use of the third-party plug-in blockskit combination;

1:nsset some common operations

Nsset *newset=[nsset setwithobjects:@"wujy",@"Cnblogs",@" Age", nil]; NSLog (@"the number of set is:%ld", [Newset Count]); //are not entered in the order in which they were added, so nsset is not sequential.Nsenumerator *enumeratorset=[Newset Objectenumerator];  for(NSObject *objinchEnumeratorset) {NSLog (@"key is:%@", obj); }        //whether there isBOOL Isexict=[newset Containsobject:@"wujy"]; NSLog (@"is present:%d", isexict); //returns any one elementNSString *anyobje=[Newset Anyobject]; NSLog (@"returns any element:%@", Anyobje); //determine if this element is included and returnNSObject *memberobj=[newset Member:@" Age"]; if(memberobj) {NSLog (@"existence Element (age):%@", Memberobj); }        //easy to create NssetNsset *twoset=[[nsset Alloc] initwitharray:@[@2,@Ten,@ A,@"wujy"]]; //determine if two nsset are equalBOOL iseaual=[Newset Isequaltoset:twoset]; NSLog (@"determine if two nsset are equal (0):%d", iseaual); //determine if two nsset intersectBOOL isintersects=[Newset Intersectsset:twoset]; NSLog (@"determine if two nsset intersect (1):%d", isintersects); //BlocksNsset*blockset=[[nsset alloc]initwithobjects:@1,@2,@3,@4,@5,@6, nil]; //Traverse[BlockSet bk_each:^(id obj) {NSLog (@"the value of the Block traversal is:%@", obj);        }]; //FilterNsset *selectset=[blockset bk_select:^bool (ID obj) {boolSelect=[obj intvalue]>3?Yes:no; return Select;    }]; NSLog (@"filtered Nsset (6,5,4):%@", SelectSet); //filtering stops only when the first one meets the criteriaNsset *matchset=[blockset bk_match:^bool (ID obj) {boolSelect=[obj intvalue]<4?Yes:no; return Select;    }]; NSLog (@"Matchset Filtered Nsset (3):%@", Matchset); //Take reverse filterNsset *rejectset=[blockset bk_reject:^bool (ID obj) {boolSelect=[obj intvalue]<4?Yes:no; return Select;    }]; NSLog (@"Reverse Filtered Nsset (6,5,4):%@", Rejectset); //to process individual itemsNsset *mapset=[blockset bk_map:^ID (id obj) {return@ ([obj intvalue]+1);    }]; NSLog (@"the modified value is (7,3,6,2,5,4):%@", mapset); //return BOOL If eligibleBOOL Isselected=[blockset bk_any:^bool (ID obj) {boolSelect=[obj intvalue]>3?Yes:no; return Select;    }]; NSLog (@"%d match condition 1", isSelected); //determine if all items meet this conditionBOOL Allselected=[blockset bk_all:^bool (ID obj) {boolSelect=[obj intvalue]>2?Yes:no; return Select;    }]; NSLog (@"%d match condition 0", allselected); //determine if all items do not meet this conditionBOOL Noneselected=[blockset bk_none:^bool (ID obj) {boolSelect=[obj intvalue]> -?Yes:no; return Select;    }]; NSLog (@"%d match condition 1", noneselected);

2:nsmutableset some common operations

    //CreateNsmutableset *mutableset=[[nsmutableset alloc]initwithcapacity:5]; [Mutableset addobject:@1]; [Mutableset addobjectsfromarray:@[@2,@3,@4]]; NSLog (@"added Nsmutableset (3,2,1,4):%@", Mutableset); //Add the Nsset in .[Mutableset Unionset:[nsset setwithobjects:@5,@6, Nil]]; NSLog (@"added Nsmutableset (3,6,2,5,1,4):%@", Mutableset); //removing elements[Mutableset removeobject:@5]; NSLog (@"removed Element (3,6,2,1,4):%@", Mutableset); //Remove the Nsset inside[Mutableset Minusset:[nsset setwithobjects:@1,@2, Nil]]; NSLog (@"nsset element after removal (3,6,4):%@", Mutableset); //Nsset converted into NsmutablesetNsset *newset=[nsset setwithobjects:@Ten,@ One,@ A, nil]; Nsmutableset*newmutableset=[nsmutablesetSet];    [Newmutableset Setset:newset]; NSLog (@"the converted Mutableset value is (12,10,11):%@", Newmutableset); //Blocks//FilterNsmutableset *blockmutableset=[[nsmutableset alloc]initwithobjects:@ -,@ at,@ -, nil]; [Blockmutableset bk_performselect:^bool (ID obj) {boolSelect=[obj intvalue]> A?Yes:no; return Select;    }]; NSLog (@"filtered Nsmutableset (25,23):%@", Blockmutableset); //Take reverse filter[Blockmutableset bk_performreject:^bool (ID obj) {boolSelect=[obj intvalue]> -?Yes:no; return Select;    }]; NSLog (@"Nsmutableset After reverse filtering:%@", Blockmutableset); //to modify an item[Blockmutableset bk_performmap:^ID (id obj) {return@ ([obj intvalue]+1);    }]; NSLog (@"modified Nsmutableset:%@", Blockmutableset);

3: Index set (Index collection) Nsindexset

//index set (Index collection) NsindexsetNsindexset * Indexset = [[Nsindexset alloc] Initwithindexesinrange:nsmakerange (1,3)];//the number in the collection is 123//extracts an element from a specified position in an array based on a collectionNsarray * array = [[Nsarray alloc] Initwithobjects:@" One",@" Both",@"three",@" Four", nil]; Nsarray* NewArray = [array objectsatindexes:indexset];//return @ "", @ "three" @ "four"//variable exponential set NsmutableindexsetNsmutableindexset *indexset =[[Nsmutableindexset alloc] init]; [Indexset Addindex:0][indexset Addindex:3]; [Indexset Addindex:5];//gets the specified element in the array by collectionNsarray *array = [[Nsarray alloc] Initwithobjects:@" One",@" Both",@"three",@" Four",@"Five",@"Six", nil]; Nsarray*newarray = [array objectsatindexes:indexset];//return @ "one" @ "four" @ "six"

iOS collection Nsset and nsmutableset knowledge points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.