#if 0
//字典:用来存储具有以一一对应关系的数据,本质上也是一个大容器
//自定义初始化方式
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"xiaoyali",@"name",@"nan",@"sex", nil];
NSString *name = [dic objectForKey:@"name"];
NSLog(@"%@",name);
NSString *sex = [dic objectForKey:@"sex"];
NSLog(@"%@",sex);
#endif
#if 0
//便利构造器方式
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaoyali",@"name",@"nan",@"sex", nil];
NSLog(@"%@",dic);
#endif
#if 0
//字面量方式
NSDictionary *dic = @{@"name":@"xiaoyali",@"sex":@"nan",@"age":@"18"};
NSLog(@"%@",dic);
//求字典的元素个数
NSUInteger count = dic.count;
NSLog(@"%lu",count);
//将所有的key放到了一个数组中
NSArray *keys = dic.allKeys;
NSLog(@"%@",keys);
//将字典中所有的value放到一个数组中
NSArray *values = dic.allValues;
NSLog(@"%@",values);
//字典取值
NSString *name = [dic objectForKey:@"name"];
NSLog(@"%@",name);
NSString *sex = [dic objectForKey:@"sex"];
NSLog(@"%@",sex);
BOOL a = dic.allKeys.count ;
NSLog(@"%d",a);
//字典本省是无序的
#endif
//可变字典
#if 0
//创建对象
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"zhangsan",@"name",@"nan",@"sex", nil];
//字典增加一个元素
[dic setObject:@"18" forKey:@"age"];
NSLog(@"%@",dic);
[dic setObject:@"jinzhou" forKey:@"city"];
NSLog(@"%@",dic);
//修改名字为xiaoyali
[dic setObject:@"xiaoyali" forKey:@"name"];
NSLog(@"%@",dic);
//setObject:forKey工作原理:先查找字典中有没有对应的key,如果有,则修改key所对应的value,如果没有则把key-value作为元素添加到字典中
[dic setObject:@"dalian" forKey:@"city"];
NSLog(@"%@",dic);
//移除元素
[dic removeObjectForKey:@"city"];
NSLog(@"%@",dic);
//删除所有元素
[dic removeAllObjects];
NSLog(@"%@",dic);
#endif
#if 1
//数组套数组
NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"11",@"22", nil];
NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"323",@"35",@"45", nil];
NSMutableArray *array = [NSMutableArray array];
[array addObject:array1];
[array addObject:array2];
NSLog(@"%@",array);
//数组里放字典
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name", nil];
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"lisi",@"name", nil];
[array addObject:dic1];
[array addObject:dic2];
NSLog(@"%@",array);
NSMutableDictionary *dic3 = array[3];
NSLog(@"%@",dic3);
//创建一个空字典
//字典里套数组
NSMutableDictionary *myDic = [NSMutableDictionary dictionary];
[myDic setObject:array forKey:@"one"];
[myDic setObject:array forKey:@"two"];
NSLog(@"%@",myDic);
//字典里套字典
[myDic setObject:dic1 forKey:@"three"];
[myDic setObject:dic2 forKey:@"four"];
NSLog(@"%@",myDic);
#endif
#if 0
//不可变集合
//特点:无序性,互异性,本质上是一个大容器
NSSet *mySet =[[NSSet alloc] initWithObjects:@"aa",@"bb",@"cc",@"dd",@"aa",@"bb",@"bb",@"cc", nil];
NSLog(@"%@",mySet);
//元素的个数
NSUInteger count = mySet.count;
NSLog(@"%lu",count);
//获取集合中的元素
NSString *string = [mySet anyObject];
NSLog(@"%@",string);
//将集合中的元素取到一个数组中
NSArray *array = mySet.allObjects;
NSLog(@"%@",array);
#endif
#if 0
//可变集合
NSMutableSet *mySet = [NSMutableSet setWithObjects:@"aa",@"bb", nil];
//添加元素
[mySet addObject:@"cc"];
[mySet addObject:@"bb"];
NSLog(@"%@",mySet);
NSLog(@"%lu",mySet.count);
//删除元素
[mySet removeObject:@"aa"];
NSLog(@"%@",mySet);
//遍历
for (NSString *string in mySet) {
NSLog(@"%@",string);
}
#endif
/*
数组,字典,集合的区别:
相同点:都是OC中的容器类,可以储存多个元素,但元素必须都是对象,不限制对象的类型.
不同点:
1.使用场景:
数组:用来管理一组有序的元素的集合.
字典:用来管理一组具有一一对应关系的数据的集合.
集合:用来管理一组无序并且具有互异性的元素的集合.
2.特点:
数组:有序,并且元素可以重复使用
字典:无序,key-value是字典中的一个元素,key是唯一的,一个key只能对应一个value,但是一个value可以对应多个key.
集合:无序,元素具有互异性,
3.取值方式:
数组:通过下标获取元素
字典:通过key值,获取元素
集合:anyObject 或得一个数组 forin遍历
*/