Objective-c Collection Object (Nsset,nsmutableset,nsindexset)

Source: Internet
Author: User



Nsarray: Ordered collection, Nsset: Unordered collection, hash store. But Nsset guarantees the uniqueness of the data. When you insert the same data, there is no effect. From the internal implementation is a hash table. Nsmutableset is a subclass of Nsset, a mutable form of nsset.


Nsset, Nsmutableset


Use of Nsset
[Nsset setwithset: (Nsset *) set]; Construct with a different set object
[Nsset Setwitharray: (Nsarray *) array];
[Nsset setwithobjects: ...]: Creates a collection object and initializes the values in the collection, which must use the nil flag at the end.
[Set Count]; Get the length of this binding object.
[Set Containsobject: ...]: Determines whether an incoming object exists in this collection and returns a bool value.
[Set Objectenumerator]: puts the collection into the iterator.
[Enumerator Nextobject]: Gets the next node data in the iterator, using the while traversal of the iterator to traverse the objects in the collection object.
[Set Isequaltoset:objset]: Determines whether two sets are exactly equal and returns a bool value.
[Set Issubsetofset:objset]: Determines whether all data in the collection is equal to the Objeset collection, and returns the bool value.
[Set allobjects];



Nsmutableset inherits Nsset, which can use the Nsset method.



[Nsmutableset Setwithcapacity:6]: Creates a Mutable collection object, and the initialization length is 6.
[Set Addobject:obj]: Adds an object dynamically to the collection.
[Set Removeobject:obj]: Deletes an object from the collection.
[Set removeallobjects]: Deletes all objects in the collection.
[Set Unionset:obj]: Adds all the data of an obj collection to the collection.
[Set Minusset:obj]: Removes all data from an obj collection to the collection.
[Set Intersectset]: Removes all data from the collection that does not contain the Obj collection.


int main (int argc, const char * argv [])
{

    @autoreleasepool {

        // Define an NSSet
        NSSet * set1 = [NSSet setWithObjects: @ "str1", @ "str2", @ "str3", nil];

        // Use NSArray to initialize an NSSet
        NSArray * array = [[NSArray alloc] initWithObjects: @ "obj1", @ "obj2", @ "obj3", @ "str1", nil];
        NSSet * set2 = [NSSet setWithArray: array];

        // Constructed with another set object
        NSSet * set3 = [NSSet setWithSet: set2];

        // variable-length collection
        NSMutableSet * set4 = [NSMutableSet setWithSet: set3];

        // Add and remove objects from the mutable collection
        [set4 addObject: @ "obj4"];
        [set4 removeObject: @ "obj2"];

        // Get the length of this combined object
        NSLog (@ "size of set1:% lu", [set1 count]);

        // determine if it contains an object
        if ([set3 containsObject: @ "str1"])
            NSLog (@ "set3 contains str1");
         else
            NSLog (@ "set3 does not contain str1");

        // Check if set1 is equal to set2
        if ([set1 isEqualToSet: set2])
            NSLog (@ "set1 equals set2");
        else
            NSLog (@ "set1 is not equal to set2");

        // Check if set1 is a subset of set2
        if ([set1 isSubsetOfSet: set2])
            NSLog (@ "set1 is a subset of set2");
        else
            NSLog (@ "set1 is not a subset of set2");

        // Get the intersection of two sets
        [set1 intersectsSet: set2];

        // Get the union of two sets
        [set4 unionSet: set3];

        // Iterate through
        for (NSObject * object in set4) {
             NSLog (@ "set4:% @", object);
        }

        // Iterate through NSEnumerator
        NSEnumerator * enumerator = [set4 objectEnumerator];
        for (NSObject * object in enumerator) {
            NSLog (@ "set4:% @", object);
        }
    }
    return 0;
} 







Nsindexset


The Nsindexset class and its mutable copy (nsmutableindexset) represent a unique collection of unsigned integers. This class is used to store an ordered index into a data structure. For example, given a Nsarray object, you can use the index in that array to determine a subset of the object.


 
 
NSIndexSet *indexSet1 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];

NSMutableIndexSet *indexSet2 = [NSMutableIndexSet indexSet];
[indexSet addIndex:2];
[indexSet addIndex:5];
[indexSet addIndex:8];

//列出NSIndexSet的值
unsigned index;
for (index = [indexSet firstIndex];
	 index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])  {
	…
}


Some methods of Nsindexset



+ (Nsindexset) indexset Create an empty collection of cables
-(BOOL) Containindex:idx returns Yes if the index collection contains the index IDX, otherwise returns no
-(Nsuinteger) count returns the number of indexes in the index collection
-(Nsuinteger) FirstIndex returns the first index in the collection, and how the collection is empty returns Nsnotfound
-(Nsuinteger) INDEXLESSTHANINDEX:IDX returns the nearest index of the collection, smaller than IDX, and returns Nsnotfound if there is no index less than IDX. Like Indexlessoreuqaltoindex:, Indexgreaterthanindex and Indexgreaterthanorequalindex:
-(Nsindexset *) Indexespassingtest: (BOOL) (^) (Nsuinteger idx,bool *stop) block blocks are applied to each element in the collection. The IDX is added to Nsindexset to return yes, otherwise no is returned. Setting the pointer variable stop is yes, which indicates interrupt handling.



Objective-c Collection Object (Nsset,nsmutableset,nsindexset)


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.