1:
NSSet
NSArray
NSDictionary
2:
NSMutableSet
NSMutableArray
NSMutableDictionary
第1组是不可变的集合,第2组是可变的集合。
可变的集合意思是:在创建后仍可向其中添加或删除元素。
不可变的集合意思是:必须在首次创建并初始化的时候提供所有值,此后不能变更。
要注意的是:NSSet,NSArray里只能添加对象,如果需要加入基本数据类型(int float BOOL bouble等),需要将数据转成NSNumber类型,再放到数组里。
例子:
NSSet *mySet = [NSSet setWithObjects:@
"one"
,@
"1111"
,[NSNumber numberWithInteger: 123], nil];
NSSet跟NSArray的区别:
1.NSSet不能向NSArray那样能通过索引值找到对应的值。
2.如果你知道某元素,想知道在集合里的位置,NSSet查找要比NSArray要效率,
---NSSet详解---
NSSet是无序的集合类,并且所存储的所有对象里的数据是唯一的,不能重复!如果重复了,它只显示一个:
NSSet *mySet = [NSSet setWithObjects:@"one",@"one",@"three",nil];
NSLog(@"mySet::%@",mySet);
输出结果是:mySet::{(one,three)}
它的长度也是2个,不是3个。
下面根据apple的帮助文档举例:
1:Creating a Set
1.1
用数组构造
例子:
NSArray *myArray = [[NSArray alloc] initWithObjects:@"1",@"2", @"3",nil];
NSSet *mySet = [NSSet setWithArray:myArray
];
NSLog(@"%@",
mySet);
输出结果是:
{(1
,2,3
)}
1.2
都是用对象构造 setWithObject只能包含一个对象,setWithObjects可以包含多个。
例子:
NSSet *mySet = [NSSet setWithObject:@"1",nil];
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",@"3",nil];
1.3
1.4
用另一个NSSet来构造
例子:
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",@"3",nil];
NSSet *mySet2 = [NSSet setWithSet:mySet];
NSLog(@"%@",mySet2);
输出结果跟mySet一样:{(1,2,3)}
1.5
添加一个对象到set
例子:
NSArray *myArray = [[NSArray alloc] initWithObjects:@"1",@"6",nil];
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",@"3",nil];
NSSet *mySet2 = [mySet setByAddingObject:@"4"];
或这里也可以添加数组
NSSet *mySet2 = [mySet setByAddingObject:myArray
];
NSLog(@"%@",mySet2);
输出结果:{(1,2,3,4)}或{(1,2,3,(1,6))}
这里有俩个1 可能是第2个1在数组元素里
1.6
添加set
例子:
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",@"3",nil];
NSSet *mySet2 = [NSSet setWithObjects:@"4",@"5",nil];
NSSet *mySet3 = [mySet setByAddingObjectsFromSe
t:mySet2];
NSLog(@"%@",mySet3);
输出结果:{(4,1,5,2,3)}
1.7
添加数组
NSArray *myArray = [[NSArray alloc] initWithObjects:@"3",@"4",@"5",nil];
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",nil];
NSSet *mySet3 = [mySet setByAddingObjectsFromAr
ray:myArray];
NSLog(@"mySet3");
输出结果:{(3,1,4,2,5)}
-----------------------------------------------------------------
2:Initializing a Set
初始化,跟上面创建不同必须分配空间。
– initWithSet:copyItems:为深层copy
3:Counting Entries
获取set的长度
例子:
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",nil];
NSLog(@"%i",mySet.count);
输出结果为:2
4:Accessing Set Members
4.1
获取set所有对象
例子:
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",@"3",nil];
NSArray *myArray = [mySet AllObjects];
NSLog(@"%@",myArray);
输出结果:{(1,2,3)}
4.2
获取set任意对象
例子:
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",@"3",nil];
id a = [mySet anyObject];
NSLog(@"%@",a);
输出结果:1
4.3
查找set里是否有该元素 有返回YES 没返回NO
例子:
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",@"3",nil];
BOOL myBool = [mySet containsObject:@"2"];
NSLog(@"%d",myBool);
输出结果是1
4.4
4.5
例子:
[myset makeObjectsPerformSelect
or:@selector(myfun)];
让myset里每一个元素都访问函数myfun
[myset makeObjectsPerformSelect
or:@selector(myfun:) withObject:@"1"];
让myset里每一个元素都访问函数myfun 并传一个参数
4.5
看set里是否有所查找得对象 有就返回该对象 没有nil
例子:
NSSet *mySet = [NSSet setWithObjects:@"1",@"2",@"3",nil];
NSLog(@"%@",[mySet member:@"1"]);
输出结果:1
4.6
返回集合中所有对象到以个NSEnumerator类型的对象
4.7
–
enumerateObjectsUsingBlo ck: –
enumerateObjectsWithOpti ons:usingBlock: –
objectsPassingTest: –
objectsWithOptions:passingTest:
代码块相关的
---------------------------------------------------------
5:Comparing Sets
这些主要是做比较
5.1
判断set2是不是set1的ziji子集(set1里是不是全部包含set2)是返回YES 不是返回NO
例子:
NSSet *mySet1 = [NSSet setWithObjects:@"1",@"2"
,@"3",
nil];
NSSet *mySet2 = [NSSet setWithObjects:@"1",@"2",nil];
NSLog(@"%d",[mySet2 isSubsetOfSet:mySet1]);
输出:1
因为mySet1里的元素 包含里mySet2里所有的元素 所以 mySet2是mySet1的子集。
5.2
两个NSSet里的元素是否有交集。是返YES 不是返NO
例子:
NSSet *mySet1 = [NSSet setWithObjects:@"1",@"2"
,@"3",
nil];
NSSet *mySet2 = [NSSet setWithObjects:@"12",@"2",nil];
NSLog(@"%d",[mySet1 intersectsSet:mySet2]);
输出:1
共同元素@"2" 有交集
5.3
两个NSSet里的元素是否完全一样。是返YES 不是返NO
NSSet *mySet1 = [NSSet setWithObjects:@"1",@"2"
,@"3",
nil];
NSSet *mySet2 = [NSSet setWithObjects:@"1",@"2",nil];
NSLog(@"%d",[mySet1 isEqualToSet:mySet2]);
输出:0
5.4