Objective-c Nsset&nsmutableset and Countedset

Source: Internet
Author: User

Nsset to tell the truth, the application of this class, still do not know when the end will be used, first over the brain, after the need to use the time, not unfamiliar!

#import <Foundation/Foundation.h>

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


@autoreleasepool {

Create 4 nsnumber objects

NSNumber *obj1 = [NSNumber numberwithint:ten];

NSNumber *obj2 = [NSNumber numberwithint:];

NSNumber *obj3 = [NSNumber numberwithfloat:10.5];

NSNumber *obj4 = [NSNumber numberwithchar:' B '];

Nsset Initialization method: Initwithobjects: The following needs to fill in the object, each object with, the number separates, commonly has two anyobject and Containsobject

nsset *aset = [[nsset alloc] initwithobjects:obj1,obj2,obj3,obj4,obj1, Nil]; collections do not allow duplication , and many obj1 write-in will not be deposited .

get the number of elements method

Nsinteger count = [ASet count];

NSLog (@ "%ld", count);

// Get Object method

NSLog(@ "= = = = = = = = = = = = = =]);

Nsarray *array = [ASet allobjects];

NSLog (@ "%@%@", array,aset);

B in Obj4 is not a direct print B, but a numeric value.

Anyobject is a convenient method of taking value

NSLog(@ "============");

nsnumber *number = [ASet anyobject];

NSLog (@ "%@", number);

convenient for all NSNumber objects

For (nsnumber *num in aSet) {

NSLog (@ "%@", num);


//Determines whether an object is contained in the collection

BOOL iscontain = [ASet containsobject:@ "AA"];

if (iscontain) {

NSLog (@ " contains ");



NSLog (@ " does not contain");


#pragma mark-nsmutableset

Nsmutableset *bset = [[Nsmutableset alloc] initwithobjects: obj1,obj2, nil];

// Add method

[BSet addobject:obj3];

[BSet addobject:obj4];

[BSet addobject: [NSNumber numberwithint:]];

NSLog (@ "%@", BSet);

// Delete a

[BSet removeobject: [NSNumber numberwithint:]];

NSLog (@ "%@", BSet);

// Delete all

[BSet removeallobjects];

NSLog (@ " all deleted %@", bSet);

#pragma mark-nscountedset

NSLog(@ "======nscountedset=======");

Nscountedset Subclass is one of the methods of extending the original class

Nscountedset *cset = [[Nscountedset alloc] initwithobjects: obj1,obj1,obj1, nil];

Get number Countforobject

Nsinteger count1 = [CSet countforobject:obj1];

NSLog (@ "%ld", count1);

//In the Nsset method we mentioned that the repeated addition of the object is not stored , the Nscountedset of the subclass can compensate for the shortcomings of the Nsset , you can record the number of times the object recurs


return 0;


Objective-c Nsset&nsmutableset and Countedset

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.