本节学习内容:
1.字典的概念
2.字典的创建及初始化
3.字典的操作
4.可变字典的创建及初始化
5.可变典的操作
【main.m】
#import<Foundation/Foundation.h>
【1.字典的概念】
//NSDictionary 创建不可变字典对象的类
//字典对象中的元素都是键值对,key:value
//字典中的元素没有顺序
//NSMutableDictionary 创建可变字典对象,继承于NSdictionary
//可以使用所有的不可变字黄类中的方法
//对于可变子典对象,可以进行添加,修改,删除操作
//在字典对象中,key的值是唯一,value的值可以相同
int main(int argc,const char*argv[]){
@autoreleasepool{
【2.字典的创建及初始化】
1.创建一个不可变对象
NSDictionary *dict=@{@"one":@"1",@"two":@"0"
,@"three":@"3",@"four":@"4"}
NSLog(@“dict=%@”,dict);
//打印结果= dict=(four=4;one=1,tree=3,two=2)
2.用传入键值对创建值对初始化字典对象,key与值要相对应
NSDictionary *dict1=[[NSDictionar allic]initWithObjectsAndKeys:@"9",@"nine",@"eight",@"7",@"seven",nil];
NSLog[@"dict1=%@",dict1];
//打印结果: dict1=(eight=8;one=1,nine=9,seven=7)
3.用传入的字典初始化字典
NSDictionary *dict2=[[NSDictionar alloc]initWithDictionary:dict];
NSLog[@"dict2=%@",dict2];
//打印结果: dict2=(four=4;one=1,tree=3,two=2)
4.用传入值的数组,及传入的key的数组创建字典对象,要求值的数组与key的数组要对应
NSDictionary *dict3=[[NSDictionar alloc]initWithObjects:[NSArray arrayWithObjects:@"1",@"2",@"2",@"4",nil] forKeys:[NSArray arrayWithObjects:@"one",@"two",@"three",@"for",nil]];
NSLog[@"dict3=%@",dict3];
//打印结果: dict2=(four=4;one=1,tree=3,two=2)
5.创建空的字典对象
NSDictionary *dict4=[[NSDictionar alloc]init]
NSLog[@"dict4=%@",dict4];
//打印结果: dict4=()
5.类方法创建字典对象,创建空的字典对象
NSDictionary *dict5=[[NSDictionar dictionary];
NSLog[@"dict5=%@",dict5];
//打印结果: dict5=()
6.类方法用传的键值创建字典对象
NSDictionary *dict6=[[NSDictionar dictionaryWithObjectsKeys:@"5",@"five",@"six",@"7",@"seven",nil];
NSLog[@"dict6=%@",dict6];
//打印结果: dict4=(five=5,seven=7,six=6)
7.类方法用传的字典对象创建字典对象
NSDictionary *dict7=[[NSDictionar dictionaryWithDictionary:dict];
NSLog[@"dict7=%@",dict7];
//打印结果: dict7=(four=4;one=1,tree=3,two=2)
8.类方法 用传的值数组与key数组 创建字典对象
NSDictionary *dict8=[NSDictionary dictionaryWithObjects:@"1",@"3",@"5",@"6"] forKeys:@[@"one",@"three",@"five",@"six"]]
NSLog[@"dict8=%@",dict8];
//打印结果: dict8=(five=5;one=1,six=6,three=3)
【3.字典的操作】
1.获取字典对象中键值对的个数
NSInteger count=[dict count];
NSLog(@"count = %li",count);
//打印结果: count = 4
2.通过key获取字典中key对应的值
id obj=[dict objectForKey:@"three"]
NSLog(@"obj = %@",obj);
//打印结果: obj = 3
3.获取所有的values
NSArray *valueArray=[dict alValues];
NSLog(@"valueArray= %@",valueArray);
//打印结果: keyArray= (1,3,2,4)
3.获取所有的key
NSArray *keyArray=[dict allkeys];
NSLog(@"keyArray= %@",keyArray);
//打印结果: keyArray= (one,three,two ,four)
4.判断两个字典对象是否相等
Bool ret=[dict isEqualToDictionary:@{@"one":@"1",@"two":@"2",@"three":@"3"}];
if(ret){
NSLog(@"字典对象相等");
}else{
NSLog(@"字典对象不相等");
}
//打印结果:字典对象不相等
5.字典的遍历[枚举器法]
NSEnumerator *keyEnumerator=[dic KeyEnumerator];
id obj2=nil;
while(obj2=[keyEnumerator nextObject]){
NSLog(@"key"=%@ value=%@",obj2,[dict objectForKey:obj2]);
}
//打印结果 key= one value=1,key= three value=3,key= two value=2, key= four value=4
6遍历字典 [快速枚举法]
for(id obj3 in dict){
NSLog(@"key"=%@ value=%@",obj3,[dict objectForKey:obj3]);
}
//打印结果 key= one value=1,key= three value=3,key= two value=2, key= four value=4
【4.可变字典的创建及初始化】
1.可变字典操作[构造指定容量大小的可变字典对象]
NSMutableDictionary *mulDict =[NSMutableDictionary alloc] initWitCapacity:20];
NSLog(@"mulDict = %@",mulDict);
//打印结果:mulDict=()
2.可变数组操作[类方法创建指定容量大小的可变字典对象]
NSMutableDictionary *mulDict1 =[NSMutableDictionary dictionaryWithCapacity:20];
NSLog(@"mulDict1= %@",mulDict1);
//打印结果:mulDict1=()
【5.可变字典的操作】
1.可变数组操作[删除键值对]
NSMutableDictionary *mulDict3 =[NSMutableDictionary dictionaryWithDictionary:dict];
NSLog(@"mulDict3= %@",mulDict3);
2.可变数组操作[删除指定key对应的键值对]
[mulDict3 removeObjectForKey:@"three"];
NSLog(@"mulDict3= %@",mulDict3);
//打印结果:mulDict3=(four=4; one=1;tow=2;)
3.可变数组操作[删除key数组对应的键值对,只删除出现在的key]
[mulDict3 removeObjectsForKey:@[@"one",@"two"]];
NSLog(@"mulDict3= %@",mulDict3);
//打印结果:mulDict3=(four=4;)
4.可变数组操作[删除所有的键值对]
[mulDict3 removeAllobjects];
NSLog(@"mulDict3= %@",mulDict3);
//打印结果:mulDict3=()
5.添加键值对[把传入字典对象的键值对添加到可变字典对象中]
[mulDict3 addEntriesFromDictionary:@{@"hello":"1",@"world":@"2",@"baidu":@"3",@"china":@"4"}];
NSLog(@"mulDict3= %@",mulDict3);
//打印结果:mulDict3=(baidu = 3; china = 4,hello = 1,world = 2);
6.重置可变字典对象方法
[mulDict3 setDictionary:dict];
NSLog(@"mulDict3= %@",mulDict3);
//打印结果:mulDict3=(four= 4; one= 1,three= 3,tow= 2);
7.添加或修改可变字典键值对
[mulDict3 setObject:@"5" forKey:@"five"];
NSLog(@"mulDict3= %@",mulDict3);
//打印结果:mulDict3=(five=5,four= 4; one= 1,three= 3,tow= 2);
//没有就是添加,有就是修改
[mulDict3 setObject:@"10" forKey:@"one"];
NSLog(@"mulDict3= %@",mulDict3);
//打印结果:mulDict3=(five=5;four= 4; one= 10;three= 3;tow= 2);
}
return 0;
}