NSArray 用于对象有序集合(相当于是数组)
NSSet 用于对象无序集合
NSDictionary用于键值映射
以上三种集合类是不可变的,一旦初始化后,就不能改变.
以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类):
NSMutableArray
NSMutableSet
NSMutableDictionary
注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int, float, double, BOOL等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。
NSDictionary用来实现字典集合,在给定关键字(通常是一个NSString字符串)下存储一个数值(可以是任何类型的对象)。
NSDictionary使用类方法 dictionaryWithObjectAndKeys: 来创建字典;使用方法objectForKey: 来获取字典中的值。
- NSDictionary *dict = [NSDictionary dictionaryWithObjectAndKeys:@"just",@"firstname",
- @"code",@"lastname",
- @"xcode@apple.com",@"email",
- nil];
- //NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
- [dict setObject:@"John" forKey:@"Firstname"];
- [dict setObject:@"Doe" forKey:@"Lastname"];
- [dict setObject:@"info@objectgraph.com" forKey:@"Email"];
- NSLog(@"%@", dict);
- NSArray *keys = [dict allKeys];
- // values in foreach loop
- for (NSString *key in keys) {
- NSLog(@"%@ is %@",key, [dict objectForKey:key]);
- }
- [dict removeObjectForKey:@"Email"];
NSArray和NSMutableArray
- NSArray *array;
- array=[NSArray arrayWithObjects:@"0-asd",@"1-fds",@"2-哈咯",@"3-个人",nil];
- //数组的长度
- NSLog(@"数组长度%d",array.count);
- //通过索引取得对象
- for(int i=0;i<array.count;i++)
- {
- NSString *secondStr=[array objectAtIndex:i];
- NSLog(secondStr,nil);
- }
- //高速枚举法取得对象,Objective-C2.0开始支持,
- for(NSString *str in array)
- {
- NSLog(str,nil);
- }
- //对象的追加于删除
- //创建空数组
- NSMutableArray *MutArray=[NSMutableArray array];
- //追加对象
- [MutArray addObject:@"A"];
- [MutArray addObjectsFromArray:array];
- //插入对象
- NSString *thstr=@"插入值";
- [MutArray insertObject:thstr atIndex:4];
- //替换对象
- [MutArray replaceObjectAtIndex:2 withObject:@"替换"];
- //删除所有对象
- //[Mutarray removeAllObjects];
- //删除最后的对象
- [MutArray removeLastObject];
- //删除索引为Index的对象
- [MutArray removeObjectAtIndex:0];
- //删除所有于object同值的对象
- [MutArray removeObject:@"0-asd"];
- //删除数组中所有与object等价的对象
- [MutArray removeObjectIdenticalTo:thstr];
- //删除数组中所有与数组array包含相同的元素
- [MutArray removeObjectsInArray:array];
- NSLog(@"%@",MutArray);
//id obj;
NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSArray *oldArray = [NSArray arrayWithObjects:
@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
NSLog(@"oldArray:%@",oldArray);
for(int i = 0; i < [oldArray count]; i++)
{
obj = [[oldArray objectAtIndex:i] copy];
[newArray addObject: obj];
}
//
NSLog(@"newArray:%@", newArray);
[newArray release];
NSSet是Object-c中使用的最少的集合类,它就如同在一个池子里放进所需要的元素。
它是无序的集合类,并且所存储的所有对象只能有唯一一个,不能重复。
它与NSArray的区别在于,它是无序的,它根据HASH算法查找特定元素,效率比遍历高。
// creating a set
NSSet *set=[NSSet set];
NSSet *set1=[NSSet setWithArray:stuArray];
NSSet *set2=[NSSet setWithObject:stu1];
NSSet *set3=[NSSet setWithObject:stuArray];
NSSet *set4=[NSSet setWithObjects:stu2,stu3,stuArray, nil];
NSSet *set5=[NSSet setWithSet:set1];
NSLog(@"set4.count=%d",[set4 count]);
for (NSString *sss in set4.allObjects)
{
NSLog(@"ssss = %@",sss);
}
setWithObjects创建包含给定的对象列表的集合
+ (id)setWithObjects:(id)firstObj ...
isEqualToSet比较两个集合是否相等
- (BOOL)isEqualToSet:(NSSet *)otherSet
containsObject判断给定的对象是否在集合中
- (BOOL)containsObject:(id)anObject
addObject给集合添加一个对象,如果已有这个对象则不会添加
- (void)addObject:(id)object
removeObject删除集合中给定的对象
- (void)removeObject:(id)anObject
intersectSet取两个集合的交集,如果接收集合中的成员不是给定集合的成员,则从接受集合中删除这个成员。
- (void)intersectSet:(NSSet *)otherSet
unionSet取两个集合的并集,如果给定集合中的成员不是接收集合的成员,则将这个成员添加到接收集合中。
- (void)unionSet:(NSSet *)otherSet
//集合比较、修改
NSSet *set1=[NSSet setWithObjects:@
"s1"
,@
"s2"
,@
"s3"
,@
"s4"
,nil];
NSMutableSet *set2=[NSMutableSet setWithObjects:@
"s1"
,@
"s2"
,@
"s3"
,@
"s4"
,nil];
NSLog(@
"List set1"
);
for
(NSString *element in set1) {
NSLog(@
"%@"
,element);
}
if
([set1 isEqualToSet:set2]) {
NSLog(@
"set1 is equal to set set2"
);
}
else
{
NSLog(@
"set1 is not equal to set set2"
);
}
if
([set1 containsObject:@
"s1"
]) {
NSLog(@
"set1 contains s1"
);
}
else
{
NSLog(@
"set1 not contains s1"
);
}
[set2 addObject:@
"s5"
];
[set2 removeObject:@
"s3"
];
NSLog(@
"List set2"
);
for
(NSString *element in set2) {
NSLog(@
"%@"
,element);
}
//集合交集、并集
NSMutableSet *set3;
set3=[NSMutableSet setWithObjects:@
"s1"
,@
"s3"
,@
"s5"
,nil];
[set3 intersectSet:set1];
NSLog(@
"135 intersectSet 1234"
);
for
(NSString *element in set3) {
NSLog(@
"%@"
,element);
}
set3=[NSMutableSet setWithObjects:@
"s1"
,@
"s3"
,@
"s5"
,nil];
[set3 unionSet:set1];
NSLog(@
"135 unionSet 1234"
);
for
(NSString *element in set3) {
NSLog(@
"%@"
,element);
}