OC中的集合类 NSSarray、NSSet、NSDictionary

NSArray 用于对象有序集合(相当于是数组)
NSSet 用于对象无序集合
NSDictionary用于键值映射
以上三种集合类是不可变的(一旦初始化后,就不能改变)

以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类):
NSMutableArray
NSMutableSet
NSMutableDictionary

注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int, float, double, BOOL等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。

 NSArray

初始化NSArray

1 NSArray *arry=[NSArray arrayWithObjects:@"asd",@"bgh",@"cui",@"dqe", nil];

判断集合的元素个数

1 NSArray *arry=[NSArray arrayWithObjects:@"asd",@"bgh",@"cui",@"dqe", nil];
2 NSLog(@"%ld",arry.count);

获取指定索引位置的集合元素

1 NSArray *arry=[NSArray arrayWithObjects:@"asd",@"bgh",@"cui",@"dqe", nil];NSString *str= [arry objectAtIndex:2];
2 NSLog(@"%@",str);

循环便利集合元素一

1 NSArray *arry=[NSArray arrayWithObjects:@"asd",@"bgh",@"cui",@"dqe", nil];
2 for (NSString *arr in arry)
3  {
4            NSLog(@"%@",arr);
5 }

NSArray的排序

 1         NSArray *arry=@[@"sunwukong",@"zhubajian",@"shawujing"];
 2         //1.选择器排序
 3         NSLog(@"排序前%@",arry);
 4         NSArray *arry1=[arry sortedArrayUsingSelector:@selector(compare:)];
 5         NSLog(@"排序后%@",arry1);
 6         //2.描述信息排序
 7         NSSortDescriptor *arry2=[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];
 8         NSArray *arry3=[arry sortedArrayUsingDescriptors:@[arry2]];
 9         NSLog(@"%@",arry3);
10         //3.代码块排序
11         NSArray *arr=[arry sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2)
12                       {
13                           return [obj1 compare:obj2];
14                       }];
15         NSLog(@"%@",arr);

 

可变集合NSMutableArray

 1           初始化可变集合
 2         NSMutableArray *arr=[NSMutableArray arrayWithCapacity:0];
 3         //添加元素
 4         [arr addObject:@"asdf"];
 5         [arr addObject:@"asdfg"];
 6         [arr addObject:@"zxbn"];
 7         //在指定位置添加元素,原有的元素会向下移
 8         [arr insertObject:@"qwerty" atIndex:1];
 9         for (NSString *str in arr) {
10             NSLog(@"%@",str);
11         }
 1         NSMutableArray *arr=[NSMutableArray arrayWithCapacity:0];
 2         NSArray *arr1=@[@"asd",@"qwe",@"zxc"];
 3         //将集合的元素赋值到可变集合中
 4         [arr1 arrayByAddingObject:arr];
 5         for (NSString *arr2 in arr1) {
 6             NSLog(@"%@",arr2);
 7         }
 8         //将集合元素拼接成字符串
 9         NSString *str=[arr1 componentsJoinedByString:@"+"];
10         NSLog(@"%@",str);
11         //将字符串拆分
12         NSArray *str1=[str componentsSeparatedByString:@"+"];
13         NSLog(@"%@",str1);
14         //清空集合
15         [arr removeAllObjects];
16         NSLog(@"%@",arr);

字典NSDictionary

 1         //字典的初始化
 2         NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"lisi",@"name",@"guiyang",@"addr", nil];
 3         //元素个数
 4         NSLog(@"%ld",dic.count);
 5         //通过关键字获取value值
 6         NSLog(@"%@",[dic objectForKey:@"name"]);
 7         NSLog(@"%@",[dic objectForKey:@"addr"]);
 8         //所有的关键字
 9         NSLog(@"%@",dic.allKeys);
10         //所有的value
11         NSLog(@"%@",dic.allValues);
12         //循环输出字典
13         for (NSString *key in dic) {
14             NSLog(@"%@->%@",key,[dic objectForKey:key]);
15         }

NSMutableDictionary

 1         NSMutableDictionary *dic1=[NSMutableDictionary dictionary];
 2         [dic1 setObject:@"lisi" forKey:@"name"];
 3         [dic1 setObject:@"guiyang" forKey:@"addr"];
 4         
 5         NSMutableDictionary *dic2=[NSMutableDictionary dictionary];
 6         [dic2 setObject:@"wangwu" forKey:@"name"];
 7         [dic2 setObject:@"zunyi" forKey:@"addr"];
 8         //把两个可变字典组合起来
 9         NSArray *arry=@[dic1,dic2];
10         
11         for (NSDictionary *dic in arry) {
12             NSLog(@"%@",[dic objectForKey:@"name"]);
13             NSLog(@"%@",[dic objectForKey:@"addr"]);
14         }

集NSSet

 1         //集 的初始化
 2         NSSet *set=[NSSet setWithObjects:@"asd",@"zxc",@"qwe", nil];
 3         //元素个数
 4         NSLog(@"%ld",set.count);
 5         NSLog(@"%@",set);
 6         //获取set中的元素
 7         NSArray *arry=[set allObjects];
 8         NSLog(@"%@",arry);
 9         //获取set中的第一个元素
10         NSString *str=[set anyObject];
11         NSLog(@"%@",str);
12         //判断set中是否包含xxx元素
13         BOOL bol=[set containsObject:@"zxc"];
14         NSLog(@"%d",bol);
15         
16         NSSet *set1=[NSSet setWithObjects:@"hjl",@"zxc",@"qwe", nil];
17         BOOL bol1=[set isEqualToSet:set1];     //判断两集是否相等
18         BOOL bol2=[set isSubsetOfSet:set1];   //判断子集
19         bool bol3=[set intersectsSet:set1];  //判断交集
20         NSLog(@"%d",bol1);
21         NSLog(@"%d",bol2);
22         NSLog(@"%d",bol3);

NSMutableSet

 1 NSMutableSet *set=[NSMutableSet set];
 2         [set addObject:@"zhangsan"];
 3         [set addObject:@"lisi"];
 4         [set addObject:@"wangwu"];
 5         //求交,并,补集
 6         NSMutableSet *set1=[NSMutableSet set];
 7         [set1 addObject:@"xiaoming"];
 8         [set1 addObject:@"lisi"];
 9         [set1 addObject:@"wangwu"];
10         [set1 addObjectsFromArray:@[@"xiaoming1",@"lisi2",@"wangwu3"]];
11         //交集
12         [set intersectsSet:set1];
13         NSLog(@"%@",set);
14         //补集
15         [set minusSet:set1];
16         NSLog(@"%@",set);
17         //并集
18         [set unionSet:set1];
19         NSLog(@"%@",set);

 

你可能感兴趣的:(OC中的集合类 NSSarray、NSSet、NSDictionary)