Common nsset Methods
The set is similar to a jar. Once the object is "thrown into" nsset set, there is no obvious order between multiple objects in the set. nsset uses the hash algorithm to store elements in the set, so it has good access and search performance.
1. Create a collection object.
<Span style = "font-size: 18px;"> nsset * nanshen = [nsset setwithobjects: @ "", @ "David", @ "", @ "", nil]; nslog (@ "% @", nanshen); </span>
The nsset set cannot contain the same elements. Two identical elements are put in, and only one element can be saved in the result.
Running result:
09:00:16. 615 oc07 _ set [520: 303] {(
"\ U6d01 \ u54e5 ",
David,
"\ U66f9 \ u5927 \ u54e5"
)}
2. Obtain the number of elements.
<span style="font-size:18px;">NSLog(@"%lu", [nanShen count]);</span>
Running result:
08:52:20. 931 oc07 _ set [447: 303] 3
3. Obtain an element in the set.
<span style="font-size:18px;"> NSLog(@"%@",[nanShen anyObject]);</span>
09:01:01. 002 oc07 _ Collection [531: 303] Jie Ge
4. Determine whether the collection contains an object.
<Span style = "font-size: 18px;"> If ([nanshen containsobject: @ ""]) {nslog (@ "");} </span>
Running result:
09:25:54. 356 oc07 _ Collection [627: 303] Brother Cao is a male
Traversal:
for (NSString *name in nanShen) { NSLog(@"%@", name); }
Running result:
09:29:19. 817 oc07 _ Collection [639: 303] Jie Ge
09:29:19. 817 oc07 _ set [639: 303] David
09:29:19. 817 oc07 _ Collection [639: 303] Brother Cao
Common nsmutableset Methods
1. Create a collection object.
<Span style = "white-space: pre"> </span> nsset * nvshen = [nsset setwithobjects: @ "Zhang Heng", @ "Chuan", @ "Jie ge ", nil]; nsmutableset * shenm = [nsmutableset setwithset: nvshen];
2. Common basic operations on a set:
Addobject: add a single element to the Collection
Removeobject: deletes a single element from a collection.
Removeallobjects deletes all elements in the set.
3. Other Common Operations
<Strong> // Union </strong> [shenm unionset: nanshen]; for (nsstring * Name in shenm) {nslog (@ "% @", name );} nslog (@ "------"); <strong> // subset </strong> If ([nvshen issubsetofset: shenm]) {nslog (@ "% @ is a subset of % @", nvshen, shenm);} else {nslog (@ "% @ is not a subset of % @", nvshen, shenm);} nslog (@ "---"); <strong> // intersection </strong> [shenm intersectset: nanshen]; for (nsstring * Name in shenm) {nslog (@ "% @", name);} nslog (@ "-1-1-1-1-1 -");
Running result:
014-09-20 09:41:06. 067 oc07 _ set [678: 303] Zhang Heng
09:41:06. 067 oc07 _ Collection [678: 303] Jie Ge
09:41:06. 067 oc07 _ set [678: 303] David
09:41:06. 068 oc07 _ Collection [678: 303] Brother Cao
09:41:06. 068 oc07 _ Collection [678: 303] Guichuan
09:41:06. 069 oc07 _ set [678: 303] ------
09:41:06. 069 oc07 _ set [678: 303] {(
"\ U6d01 \ u54e5 ",
"\ U5f20 \ u6052 ",
"\ U8d35 \ u5ddd"
)} Is {(
"\ U5f20 \ u6052 ",
"\ U6d01 \ u54e5 ",
David,
"\ U66f9 \ u5927 \ u54e5 ",
"\ U8d35 \ u5ddd"
)} Subset
09:41:06. 070 oc07 _ set [678: 303]---
09:41:06. 070 oc07 _ Collection [678: 303] Jie Ge
09:41:06. 070 oc07 _ set [678: 303] David
09:41:06. 071 oc07 _ Collection [678: 303] Brother Cao
09:41:06. 071 oc07 _ set [678: 303]-1-1-1-1-1-1-
OC set lesson7 on the seventh day