NSSet and NSMutableSet in Objective-C syntax

Source: Internet
Author: User

NSSet and NSMutableSet are unordered, But they guarantee data uniqueness. When the same data is inserted, it will not have any effect. The internal implementation is a hash table, so you can search for a data within a constant time.

1. Use of NSSet
[NSSet setWithSet :( NSSet *) set]; constructed with another set object
[NSSet setWithArray :( NSArray *) array]; constructed using Arrays
[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 isequtoset: objset]: returns the Bool value if two sets are completely equal.
[Set isSubsetOfSet: objset]: checks whether all data in the set is equal to that in the objeset set, and returns the Bool value.
[Set allObjects];

Sample Code:

1.1 construct a set with NSArray

NSArray * array = [[NSArray alloc] initWithObjects: @ "Object abc", @ "rongfzh", @ "totogo2010", nil]; NSSet * set3 = [NSSet setWithArray: array]; NSLog (@ "% @", set3 );

Print:

2012-07-10 09:39:02.015 objectiveC[720:403] {(    rongfzh,    "\U5bf9\U8c61abc",    totogo2010)}

1.2 use of some comparison methods of set.

Int main (int argc, const char * argv []) {@ autoreleasepool {NSSet * set = [NSSet setWithObjects: @ "25", @ "age", @ "Zhang San ", @ "name", @ "male", nil]; NSSet * set1 = [NSSet setWithObjects: @ "25", @ "age", @ "James ", @ "name", @ "male", @ "gender", nil]; NSLog (@ "set count: % lu", [set count]); // determine whether an age string is contained. if ([set containsObject: @ "age"]) {NSLog (@ "set includes age ");} // determine whether set is equal to set1 if ([set isEqualToSet: set1]) {NSLog (@ "set is equal to set1 ");} // determine whether the set is a set1 sub-set if ([set isSubsetOfSet: set1]) {NSLog (@ "set isSubsetOfSet set1 ");} // obtain all set objects NSArray * array = [set allObjects]; NSLog (@ "array: % @", array ); // iteratively traverse NSEnumerator * enumerator = [set objectEnumerator]; for (NSObject * object in enumerator) {NSLog (@ "set1 object: % @", object );}} return 0 ;}

Print result:

09:50:32. 018 objectiveC [939: 403] set count: 52012-07-10 09:50:32. 020 objectiveC [939: 403] set contains age2012-07-10 09:50:32. 021 objectiveC [939: 403] set isSubsetOfSet set12012-07-10 09:50:32. 023 objectiveC [939: 403] array :( age, 25, "\ U7537", "\ U5f20 \ U4e09", name) 09:50:32. 027 objectiveC [939: 403] set1 object: age2012-07-10 09:50:32. 028 objectiveC [939: 403] objects in set1: 252012-07-10 09:50:32. 028 objectiveC [939: 403] objects in set1: Male 09:50:32. 029 objectiveC [939: 403] objects in set1: Zhang San 09:50:32. 029 objectiveC [939: 403] object in set1: name

2. Use of NSMutableSet

NSMutableSet inherits NSSet, which can use the NSSet method.

[NSMutableSet setWithCapacity: 6]: Creates a variable set object and the initialization length is 6.
[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.

Int main (int argc, const char * argv []) {@ autoreleasepool {NSMutableSet * muSet = [NSMutableSet setWithCapacity: 6]; [muSet addObject: @ "Object 1"]; NSSet * set = [NSSet setWithObjects: @ "Object 2", @ "Object 3", @ "Bite by Penguin", nil]; // Add set data [muSet unionSet: set]; for (NSObject * object in muSet) {NSLog (@ "all nuSet: % @", object );} NSSet * set1 = [NSSet setWithObjects: @ "Object 2", @ "Object 3", nil]; // Delete the total data containing set1 in muSet [muSet minusSet: set1]; for (NSObject * object in muSet) {NSLog (@ "after minusSet: % @", object) ;}} return 0 ;}

Print result:

10:09:08. 194 objectiveC [1156: 403] all nuSet: Object 12012-07-10 10:09:08. 196 objectiveC [1156: 403] all nuSet: taken by penguins 10:09:08. 196 objectiveC [1156: 403] all nuSet: Object 22012-07-10 10:09:08. 197 objectiveC [1156: 403] all nuSet: Object 32012-07-10 10:09:08. 198 objectiveC [1156: 403] after minusSet: Object 12012-07-10 10:09:08. 198 objectiveC [1156: 403] after minusSet: taken by penguins

  

Related Article

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.