Objective-c----Nsset, Nsmutableset, Nscountedset

Source: Internet
Author: User

Directly on the code:
/ * * Nsset Immutable Collection * */        //Two types of initialization methodsNsset *set1 = [[Nsset alloc] initwithobjects:@"1", @"2", @"3",Nil] ;NSLog( @"%@", Set1); Nsset *set2 = [Nsset setwithobjects:@" A", @"All", @" the",Nil] ;NSLog( @"%@", Set2);//Using an array object to create a collection object        Nsarray*array = @[@1, @2, @2] ;//initwitharray and Setwitharray convert the array object to a collection object, which filters out duplicate objects in the arrayNsset *set3 = [[Nsset alloc] initwitharray:array];NSLog( @"%@", Set3); Nsset *set4 = [Nsset Setwitharray:array];NSLog( @"%@", SET4);//Gets the number of objects in the collection        NSLog( @"%ld", [Set4 Count]);//Gets the object in the collection (returns a single object that returns nil if there are no objects in the collection)        IDObject1 = [Set4 anyobject];NSLog( @"%@", Object1);//Determines whether a given object is contained in the specified collection        NSString*RESULT1 = [Set4 containsobject:@2] ? @"YES": @"NO";NSLog( @"%@ is contained int set%@", @2, RESULT1);///@2 to @ "2" Results printed NO//NSString *RESULT1 = [Set4 containsobject:@ "2"]? @ "YES": @ "NO";//NSLog (@ "%@ is contained int set%@" @ "2", RESULT1);        / * * nsmutableset Variable set * */        //InitializeNsmutableset *mutableset1 = [[Nsmutableset alloc] init];NSLog( @"%@", MutableSet1); Nsmutableset *mutableset2 = [Nsmutableset set];NSLog( @"%@", MutableSet2);//created with immutable objectsNsmutableset *mutableset3 = [[Nsmutableset alloc] initwithset:set1];NSLog( @"%@", MutableSet3); Nsmutableset *mutableset4 = [Nsmutableset Setwithset:set1];NSLog( @"%@", MUTABLESET4);//Add collection elements (Note: @4 and @ "4" are not the same)[MutableSet4 addobject:@4] ;NSLog( @"%@", MUTABLESET4);//Delete a single collection element[MutableSet4 removeobject:@4] ;NSLog( @"%@", MUTABLESET4);//Delete all collection elements[MutableSet4 removeallobjects];NSLog( @"%@", MUTABLESET4);/* * nscountedset * * is a subclass of Nsset that can record the number of repetitions of elements in the collection */        //Nscountedset *countset1 = [Nscountedset set]; [CountSet1 addobject:@1] ; [CountSet1 addobject:@2] ; [CountSet1 addobject:@3] ; [CountSet1 addobject:@2] ;NSLog( @"%@", CountSet1);//Gets the number of times an object has occurred in the collection individually//NSLog (@ "%ld", [CountSet1 countofobjc:@3]);        NSLog( @"%ld", [CountSet1 countforobject:@5] ) ;

Objective-c----Nsset, Nsmutableset, Nscountedset

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.