开发工作

NSSet 和 NSMutableSet详解

2016-06-27  本文已影响1042人  南巷旧人M

作者:孟令文

刚刚学习了Funcdation框架中的NSSet,跟大家分享一下。

1、集合:集合(NSSet)和数组(NSArray)有相似之处,都是存储不同的对象的地址;不过NSArray是有序的集合,NSSet是无序的集合。

集合是一种哈希表,运用散列算法,查找集合中的元素比数组速度更快,但是它没有顺序。

2、存储的所有对象只能有唯一一个,不能重复。

/****************    Immutable Set    ****************/

NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", @"a", @"b", @"c", @"d",nil];

//直接类名调用,不用alloc。+号方法加号方法使用一组对象创建新的集合

NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil];

//—号方法

NSSet *set2 = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil];

//把数组转化成集合

NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c",@"d", nil];

NSSet *set3    = [NSSet setWithArray:array];

//打印集合中的所有元素

NSLog(@"set:%@",set);

NSLog(@"set1 :%@", set1);

NSLog(@"set2 :%@", set2);

NSLog(@"set3 :%@", set3);

//获取集合个数

NSLog(@"set count :%lu", set1.count);

NSLog(@"set count :%lu", [set1 count]);

//以数组的形式获取集合中的所有对象

NSArray *allObj = [set3 allObjects];

NSLog(@"allObj :%@", allObj);

//是否包含某个对象

NSLog(@"Is set3 coatains a ?  %d", [set3 containsObject:@"a"]);

//是否包含指定set中的对象

NSLog(@"Is set1 contains set3's obj ?  %d", [set1 intersectsSet:set3]);

//是否完全匹配

NSLog(@"set1 isEqualto set3 :%d", [set1 isEqualToSet:set3]);

NSLog(@"set2 isEqualto set3 :%d", [set2 isEqualToSet:set3]);

//是否是子集合

NSLog(@"set3 isSubSet of set1:%d", [set3 isSubsetOfSet:set1]);

NSLog(@"set3 isSubSet of set :%d", [set3 isSubsetOfSet:set ]);

//set 加一个 arrar 类型的对象

NSSet *set4 = [NSSet setWithObjects:@"a", @"b", nil];

NSArray *ary = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];

NSSet *set5 = [set4 setByAddingObjectsFromArray:ary];

NSLog(@"addFromArray :%@", set5);

//set 加一个 id 类型的对象

NSSet *set6 = [set5 setByAddingObject:@"c"];

NSLog(@"set6 is :%@",set6);

//set 加一个 set 类型的对象

NSSet *set7 = [set1 setByAddingObjectsFromSet:set2];

NSLog(@"set7 is :%@",set7);

/****************    Mutable Set    ****************/

//初始化

NSMutableSet *mutableSet = [[NSMutableSet alloc] init];

[mutableSet addObject:@"1"];

NSLog(@"mutableSet1 is :%@",mutableSet);

//加号方法使用一组对象创建新的集合

NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"1", @"2", @"3", nil];

NSLog(@"mutableSet1 is %@",mutableSet1);

//初始化一个新分配的集合,大小为size

NSMutableSet *mutableSet2 = [[NSMutableSet alloc] initWithCapacity:3];

[mutableSet2 addObject:@"1"];

[mutableSet2 addObject:@"b"];

[mutableSet2 addObject:@"3"];

//创建一个有size大小的新集合

NSMutableSet *mutableSet3 = [NSMutableSet setWithCapacity:3];

[mutableSet3 addObject:@"a"];

[mutableSet3 addObject:@"2"];

[mutableSet3 addObject:@"c"];

//集合元素相减

[mutableSet1 minusSet:mutableSet2];

NSLog(@"minus :%@", mutableSet1);

//只留下相等元素, 做交集运算

[mutableSet2 intersectSet:mutableSet3];

NSLog(@"intersect :%@", mutableSet2);

//合并集合 将两个集合中所有元素添加到调用者

[mutableSet2 unionSet:mutableSet3];

NSLog(@"union :%@", mutableSet2);

//删除指定元素

[mutableSet2 removeObject:@"a"];

NSLog(@"removeObj :%@", mutableSet2);

//删除所有数据

[mutableSet2 removeAllObjects];

NSLog(@"removeAll :%@", mutableSet2);

//清空接收,把自己清空然后接受另一个set传过来的所有对象

NSMutableSet *mutableSet4 = [NSMutableSet setWithObjects:@"a",@"b",@"c" ,nil];

[mutableSet4 setSet:set2];

NSLog(@"removeAll :%@", mutableSet4);

/****************    Counted Set    ****************/

//NSCountedSet类声明一个可变的编程接口,无序模糊对象的集合。一组数也称为一个袋子。概述每个不同的对象插入一个NSCountedSet对象有一个与之关联的计数器。

NSCountedSet *countedSet = [[NSCountedSet alloc] initWithObjects:@"1",@"2",@"3",@"2",@"1", nil];

NSLog(@"countedSet is :%@",countedSet);

上一篇下一篇

猜你喜欢

热点阅读