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 ");
}Else
{
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