Objective-C Syntax: What about a set object?
If the original Yusong Momo article is reprinted, please note: It is reprinted to my independent domain name blogYusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/425
The key words of the Set object are nsset and nsmutableset. The former is an immutable set, and the latter is a mutable set. In objective-C, most data structures have two types of data: variable and immutable. The set here is similar to the set in Java and C. The usage is basically the same. However, in objective-C, the same set can store data of different objects. The following is a clear example.
1. Create an unchangeable set object
[Nsnumber numberwithinteger: 9527]; because the collection can only store objects, if you want to save a number, you must convert the number into an object before saving it.
[Nsset setwithobjects:...]: Creates a set object and initializes a value in the set. The NIL flag must be used at the end.
[Set count]; get the length of the combined object.
[Set containsobject:...]: determines whether an input object exists in this set and returns the bool value.
[Set objectenumerator]: puts the set into the iterator.
[Enumerator nextobject]: to obtain the data of the next node in the iterator, use the while traversal tool to traverse the objects in the Set object.
[Set isequaltoset: objset]: determines whether the two sets are completely equal.
,
Returns the bool value.
[
Set issubsetofset: objset
]: Determines whether all data in the set is equal to that in the objeset set,
Returns the bool value.
[Set istersectset:
Objset
]:
Determine that at least one data in the set is equal to that in the objeset set,
Returns the bool value.
# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code // create a collection nsset * Set = [nsset setwithobjects: @ "Yu Song Momo", @ "15810463139", [nsnumber numberwithinteger: 9527], nil]; // number of sets int COUNT = [set count]; nslog (@ "number in the Set: % d", count ); // check whether the collection contains nsstring * STR = @ "Yu Song Momo"; if ([set containsobject: Str]) {nslog (@ "contains % @ This object ", STR);} // iterator traverses nsenumerator * enumerator = [set objectenumerator]; nsobject * object = [enumerator nextobject]; while (object! = Nil) {nslog (@ "iterator traversing data in the Set: % @", object); object = [enumerator nextobject];} // quick enumeration traversal for (nsobject * object in set) {nslog (@ "quick enumeration traversal of data in the Set: % @", object);} int retval = uiapplicationmain (argc, argv, nil, nil); [pool release]; return retval ;}
2. Create a variable set object
Nsmutableset inherits nsset, so you can use the parent class method.
[Nsmutableset setwithcapacity: 10]: Creates a variable set object and the initialization length is 10.
[Set addobject: OBJ]: dynamically add objects to a set.
[Set removeobject: OBJ]: deletes an object in the set.
[Set removeallobjects]: deletes all objects in the set.
[Set unionset: OBJ]: Adds all data of an OBJ set to the set.
[Set minusset: OBJ]: deletes all data of an OBJ set from the set.
[Set intersectset]: deletes all data that does not contain the OBJ set from the collection.
# Import <uikit/uikit. h> # import "myclass. H "int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; // Add our test code // create a set nsmutableset * Set = [nsmutableset setwithcapacity: 10]; // Add data [set addobject: @ "Yu Song Momo"]; [set addobject: [nsnumber numberwithint: 9527]; // Add data to delete nsstring * STR = @ "delete me"; [set addobject: Str]; // delete it [set removeobject: Str]; // fast enumeration traversal for (nsobject * object in set) {nslog (@ "quick enumeration traversing data in the Set: % @", object);} int retval = uiapplicationmain (argc, argv, nil, nil); [pool release]; return retval ;}
The beauty of the set, not an explanation! Wow ~ Good night !!!