/*
//数组 + 字典 + 集 = 集合
//键(key) 值(Value) 对 是一个整体
//输出大括号是字典,小括号是数组
//不可变字典
//创建一个字典
//1.正常的初始化方法
NSDictionary *dict1=[[NSDictionary alloc] initWithObjectsAndKeys:@"张三",@"name",@"男",@"sex",@"18",@"age", nil];
//输出方法一(创建方式1)
NSLog(@"%@",dict1);//哈希hash排序输出,汉字输出的是4位数的编码
//输出方法二(创建方式2)
//使用数组创建
NSArray *keyArray=[NSArray arrayWithObjects:@"name",@"sex",@"age", nil];
NSArray *valueArray=[NSArray arrayWithObjects:@"张三",@"男",@"18", nil];
//将两个数组放在一个字典里面
NSDictionary *dict2=[[NSDictionary alloc] initWithObjects:valueArray forKeys:keyArray];
NSLog(@"%@",dict2);
//输出方法3(创建方式3)
//使用字典创建字典
NSDictionary *dict3=[NSDictionary dictionaryWithDictionary:dict2];
NSLog(@"%@",dict3);
//字典的使用(常用方法)
//取出字典中所有的key值
NSArray *arr1=[dict3 allKeys];
NSLog(@"%@",arr1);
//取出字典中所有的value值
NSArray *arr2=[dict3 allValues];
NSLog(@"%@",arr2);
//根据指定key值 返回key对应的value值
NSString *s1=[dict3 objectForKey:@"name"];
NSLog(@"%@",s1);
//value值可以是任意类类型 key值: NSSring类型
//能够当key值的,都需要遵循NSCopying协议
Person *p=[[Person alloc] initWithName:@"张三" age:18];
Person *p1=[[Person alloc] initWithName:@"李四" age:20];
Person *p2=[[Person alloc] initWithName:@"王二" age:25];
NSArray *keyArray1=[NSArray arrayWithObjects:@"张三",@"李四",@"王二" ,nil];
NSArray *valueArray1=[NSArray arrayWithObjects:p,p1,p2,nil];
NSDictionary *dict4=[[NSDictionary alloc] initWithObjects:valueArray1 forKeys:keyArray1];
NSLog(@"%@",dict4);
NSLog(@"%@",p);
//可变字典(继承于不可变字典)
//创建一个空的字典
NSMutableDictionary *mdict1=[NSMutableDictionary dictionary];
//添加元素
[mdict1 setObject:@"Tom" forKey:@"name"];
NSLog(@"%@",mdict1);
//删除元素
[mdict1 removeObjectForKey:@"name"];
NSLog(@"%@",mdict1);
//往空字典添加三个元素
[mdict1 setObject:@"a" forKey:@"1"];
[mdict1 setObject:@"b" forKey:@"2"];
[mdict1 setObject:@"c" forKey:@"3"];
NSLog(@"%@",mdict1);
//修改元素:首先找key值,看有没有key值,如果有就用新value值修改value值,如果没有那就直接添加(根据key改value)
//有则改之,无则加勉.
[mdict1 setObject:@"x" forKey:@"1"];
NSLog(@"%@",mdict1);
*/
/*
Student *stu1=[[Student alloc] initWithName:@"tony"];
NSLog(@"%@",stu1);
*/
/*
Person *person1=[[Person alloc] initWithName:@"tony" age:20];
Person *person2=[[Person alloc] initWithName:@"mike" age:18];
Person *person3=[[Person alloc] initWithName:@"joek" age:25];
//初始化一个空字典
NSMutableDictionary *mdict1=[NSMutableDictionary dictionary];
[mdict1 setObject:person1 forKey:[person1 name]];
[mdict1 setObject:person2 forKey:[person2 name]];
[mdict1 setObject:person3 forKey:[person3 name]];
NSLog(@"%@",mdict1);
//数组,字典嵌套
NSMutableDictionary *mdict2=[NSMutableDictionary dictionaryWithDictionary:mdict1];
NSArray *arr1=[NSArray arrayWithObjects:mdict1 ,mdict2,nil];
NSDictionary *dict1=[NSDictionary dictionaryWithObject:arr1 forKey:@"person"];
NSLog(@"%@",dict1);
NSArray *arr2=[dict1 valueForKey:@"person"];//用数组接收
NSDictionary *dict2=arr2[0];//用字典接收
Person *p=[dict2 valueForKey:@"mike"];
NSLog(@"%@",p);