创建字典对象
NSDictionary *dic=[[NSDictionary alloc]init];
便利构造器创建
NSDictionary *dic=[NSDictionary dictionary];
// NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"2",@"3",@"4", nil];
// NSLog(@"%@",dic);
// key是唯一的,但是value可以重复
// key不能为空
当前有多少键值对
NSLog( @"%ld",dic.count);
通过对应的key找到对应的value
NSLog(@"%@",[dic objectForKey:@"2"]);
NSLog(@"%@",dic[@"2"]);
获取字典所有的key和所有的value
NSLog(@"%@",dic.allKeys);
NSLog(@"%@",dic.allValues);
可变的字典
NSMutableDictionary *dic=[NSMutableDictionary dictionary];
NSMutableDictionary *dic1=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"2",@"3",@"4",@"5",@"6", nil];
对可变的字典进行键值对的添加
[dic setObject:@"7" forKey:@"8"];
// 当key存在的时候是对key为@"8"的值进行修改
[dic setObject:@"1000" forKey:@"8"];
NSLog(@"%@",dic);
移除键值对
[dic1 removeObjectForKey:@"4"];
[dic1 removeAllObjects];
setobject是字典自己的方法,setvalue是kvc的方法,即使效果一样,但以后要对字典进行操作,还是用setobject
[dic setValue:@"100" forKey:@"nihao"];
for循环怎么去循环字典dic
for(NSInteger i=0;i<dic1.allKeys.count;i++)
{
NSLog(@"%@",dic.allValues[i]);
}
快速枚举
for (NSString *str in dic1) {
NSLog(@"%@",str);
}
// 打印出来的是key值
找到字典对应的value
for (NSString *str in dic1) {
NSLog(@"%@",dic1[str]);
}
id就是void * 如果类型不确定的话可以用id 但是确定的话直接用类型就好了
for (id obj in dic1) {
NSLog(@"%@",obj);
}
字典里面套数组
NSArray *arr1=@[@"1",@"2",@"3",@"4"];
NSArray *arr2=@[@"5",@"6",@"7",@"8"];
NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:arr1,@"arr1",arr2,@"arr2", nil];
// 遍历
for (NSString *str in dic)// key的类型
{
// 针对对应的key找到value,value是一个数组,所以用一个数组对象来接收
NSArray *arr=dic[str];
for (NSString *temp in arr)// 数组里的每个元素都是字符串类型的
{
NSLog(@"%@",temp);
}
}
数组里的对象是字典
NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:@"杨林",@"name",@"19",@"age", nil];
NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"刘珊珊",@"name",@"20",@"age", nil];
NSDictionary *dic3=[NSDictionary dictionaryWithObjectsAndKeys:@"商帅",@"name",@"24",@"age", nil];
NSArray *arr=@[dic1,dic2,dic3];
// 遍历
for (NSDictionary *dic in arr) {
for (NSString *key in dic) {
NSLog(@"%@",dic[key]);
}
}
练习
NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:@"杨林",@"name",@"19",@"age", nil];
NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"刘珊珊",@"name",@"20",@"age", nil];
NSDictionary *dic3=[NSDictionary dictionaryWithObjectsAndKeys:@"商帅",@"name",@"24",@"age", nil];
NSArray *arr1=@[dic1,dic2];
NSArray *arr2=@[dic3];
NSDictionary *bigDic=[NSDictionary dictionaryWithObjectsAndKeys:arr1,@"arr1",arr2,@"arr2", nil];
// forin循环 最外面一层是bigDic 第二层是两个数组 第三层是字典
for (NSString *str in bigDic)// 第一层的key值是一个字符串类型,所以用NSString *str来表示
{
NSArray *arr=bigDic[str];
for (NSMutableDictionary *dic in arr) {
for (NSString *temp in dic) {
NSLog(@"%@",dic[temp]);
}
}
}
练习
NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:@"杨林",@"name",@"19",@"age", nil];
NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"刘珊珊",@"name",@"20",@"age", nil];
NSDictionary *dic3=[NSDictionary dictionaryWithObjectsAndKeys:@"商帅",@"name",@"24",@"age", nil];
NSArray *arr1=@[dic1,dic2];
NSArray *arr2=@[dic3];
NSArray *bigArr=@[arr1,arr2];
// 第一层是数组,第二层为数组,第三层为字典
for (NSArray *arr in bigArr) {
for (NSMutableDictionary *dic in arr) {
for (NSString *str in dic) {
NSLog(@"%@",dic[str]);
}
}
}
定义三本书的字典
NSMutableDictionary *book1=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"三国演义",@"name",@"100",@"price", nil];
NSMutableDictionary *book2=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"水浒传",@"name",@"200",@"price", nil];
NSMutableDictionary *book3=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"西游记",@"name",@"300",@"price", nil];
NSMutableArray *arr=[NSMutableArray arrayWithObjects:book1,book2,book3, nil];
// 2 数组可以添加 删除书籍
// 首先定义一个book类字典
NSMutableDictionary *book4=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"HarryPotter",@"name",@"77",@"price", nil];
[arr addObject:book4];
[arr removeObject:book1];
for (NSMutableDictionary *dic in arr) {
for (NSString *str in dic) {
NSLog(@"%@",dic[str]);
}
}
[arr removeObject:book1];
// 可以从数组根据书名查找书籍,并修改书籍的价格
for (NSMutableDictionary *dic in arr) {
if ([dic[@"name"]isEqualTo:@"水浒传"]) {
[dic setObject:@"200" forKey:@"price"];
}
}