Collection classes in Objective-c

Source: Internet
Author: User

The collection class in iOS is like a collection in mathematics, where elements must be unique, storage elements are unordered, and storage elements must be object types.

The set is divided into immutable sets (Nsset) and mutable sets (Nsmutableset)

Immutable collection (Nsset)

To create a collection object

1 1Nsset *Set= [Nsset setwithobjects:@"a",@"P",@"P", nil];2 2NSLog (@"set:%@",Set);3 //gets the number of elements in the collection4 3NSLog (@"Count:%ld",Set. Count);5 //gets an element in the collection6 4NSLog (@"object:%@", [SetAnyobject]);

Because the total element of the collection is unique, the count value printed at this point is 2.

Determines whether an object is contained in the collection

1 if ([Set containsobject:@ "a"]) {2        NSLog ( " contains the object in the collection "); 3     Else {4         NSLog ( " The object is not included in the collection "); 5     }

Variable set (nsmutableset)

To create a collection object

1Nsmutableset *mset = [NsmutablesetSet];2 //adding elements3[MSet AddObject:@"I"];4[MSet AddObject:@"O"];5[MSet AddObject:@"S"];6NSLog (@"MSet:%@", MSet);

Delete Element

1 // Delete 2 [mSet removeobject:@ "i"]; 3 NSLog (@ "mSet:%@", MSet);

Count Set (Nscountedset)

Nscountedset is a subclass of Nsmutableset that can record the number of repetitions of an element, adding a count function based on set.

1 //Create a collection2Nscountedset *cset = [NscountedsetSet];3 //adding elements4[CSet AddObject:@"a"];5[CSet AddObject:@"P"];6[CSet AddObject:@"P"];7NSLog (@"CSet:%@", CSet);8NSLog (@"Count:%ld", cset.count);9 //the number of times the element was added in the collectionTenNSLog (@"Count:%ld", [CSet Countforobject:@"P"]);

Collection classes in Objective-c

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.