In the foundation framework, a set of Single-value objects is provided, and the elements in the nsset instance are unordered. Only one object can be saved, similar to the hashset in Java. Java and OC are too similar.
1. Continue to be immutable, nsset
Nsset is similar to array creation.Objective-CMediumNssetAndNsarrayThe difference between them will be detailed later.
nsset * set1 = [nsset setwithobjects: @ "one", @ "two", @ "three" Nil]; // Similarly, nil cannot be less. // create nsarray * array = [nsarray arraywithobjects: @ "hello", @ "world", @ "hao", nil] Through arrays; nsset * set2 = [nsset setwitharray: array]; // construct nsset * set3 = [nsset setwithset: set2] through an existing set; // The number of objects in the Set nsinteger * objnum = [set3 count]; // return all objects in the set in the form of arrays nsarray * array1 = [set3 allobjects]; // check whether the collection contains an object bool iscontain = [set3 containsobject: @ "one"]; // whether the set2 set is the sub-set bool issub = [set2 issubsetofset: set3]; // whether the two sets are the same bool isequal = [set3 is1_toset: set2]; // Add an element based on the original set to create a new set nsset * set4 = [set3 setbyaddingobject: @ "Yade"]; // combine the two sets into a new set nsset * set5 = [set3 setbyaddingobjectsfromset: set2]; // merge an array of a set into a new set nsset * set6 = [set5 setbyaddingobjectsfromarray: array];
2. Variable set nsmutableset
Inherited from nsset
// Create two statements first. Then nsmutableset * mset1 = [nsmutableset setwithobjects: @ "one", @ "two", @ "1", nil]; nsmutableset * mset2 = [nsmutableset setwithobjects: @ "three", @ "four", @ "1" Nil]; // remove the same elements as mset2 in mset1, the returned result is mset2 [mset2 minusset: mset1]; // get the intersection of the two sets, and the returned result is mset1 [mset1 intersectset: mset2]; // get the Union set [mset1 unionset: mset2]; // set the content in one set to [mset1 setset: mset2] in another set; // Delete the object [mset1 removeobject: @ "one"]; // Delete All [mset1 removeallobjects];