OC基本框架之-字典类型
分类:NSDictionary(不可变数据字典)和NSMutableDictionary(可变数据字典)
组成:
(2)任何对象(id类型)都可以作键,但一般是字符串
(3)字典内元素无序排列,键相同的的元素不能重复
1 //1、用一个或多个键值对初始化一个字典对象: 以值,建,值,键,...,nil的顺序
2 NSDictionary *dict01=[[NSDictionary alloc] initWithObjectsAndKeys:@"key0",@"value0",@"key1",@"value1",@"key2",@"value2", nil];
3 NSLog(@"dict01:%@",dict01);
4 NSDictionary *dict1=[NSDictionary dictionaryWithObjectsAndKeys:@123,@"key0",@456,@"key1",@678,@"key2", nil];
5 NSLog(@"dict1:%@",dict1);
6 //2、用两个数组初始化一个字典,一个key数组,一个value数组,两个数组大小必须相等,否则Crash
7 NSArray *keyArray=@[@"name0",@"name1",@"name2"];
8 NSArray *valueArray=@[@"hong",@"wang",@"zhao"];
9 NSDictionary *dict02=[[NSDictionary alloc] initWithObjects:keyArray forKeys:valueArray];
10 NSDictionary *dict2=[NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
11 NSLog(@"dict02:%@",dict02);
12 NSLog(@"dict2:%@",dict2);
13
14 //3、用一个数据字典初始化另一个数据字典
15 NSDictionary *dict03=[[NSDictionary alloc] initWithDictionary:dict1];
16 NSDictionary *dict3=[NSDictionary dictionaryWithDictionary:dict1];
17 NSLog(@"dict03:%@",dict03);
18 NSLog(@"dict3:%@",dict3);
(2)字典对象的创建和保存-文件
//1、保存字典对象内容到文件
[dict3 writeToFile:@"/Users/apple/Desktop/tesk.txt" atomically:YES];
//2、从之前保存的文件读取数据到字典对象
NSDictionary *dict4=[NSDictionary dictionaryWithContentsOfFile:@"/Users/apple/Desktop/tesk.txt"];
NSLog(@"dict4:%@",dict4);
(3)字典对象元素的访问
1 //1、分别获取字典的所有键、值
2 NSArray *allkeys=[dict1 allKeys];
3 NSLog(@"allkeys:%@",allkeys);
4 NSArray *allvalues=[dict1 allValues];
5 NSLog(@"allvalues:%@",allvalues);
6
7 //2、通过指定的key取对应的value对象,没有这个key的话.返回nil
8 NSNumber *num1 = [dict1 objectForKey:@"key1"];
9 NSNumber *num2 = [dict1 objectForKey:@"key5"];
10 NSLog(@"%@",num1);
11 NSLog(@"%@",num2);
12
13 //3、获取字典中键值对的个数
14 NSUInteger paircount=[dict2 count];
15 NSLog(@"%lu",paircount);
(4)字典的遍历
1 for (id key in dict1) {
2 id value=[dict1 objectForKey:key];
3 NSLog(@"%@=%@",key,value);
4 }
2015-04-07 21:17:53.933 NSDictionary[5465:226991] dict01:{ value0 = key0; value1 = key1; value2 = key2; } 2015-04-07 21:17:53.934 NSDictionary[5465:226991] dict1:{ key0 = 123; key1 = 456; key2 = 678; } 2015-04-07 21:17:53.934 NSDictionary[5465:226991] dict02:{ hong = name0; wang = name1; zhao = name2; } 2015-04-07 21:17:53.934 NSDictionary[5465:226991] dict2:{ name0 = hong; name1 = wang; name2 = zhao; } 2015-04-07 21:17:53.935 NSDictionary[5465:226991] dict03:{ key0 = 123; key1 = 456; key2 = 678; } 2015-04-07 21:17:53.935 NSDictionary[5465:226991] dict3:{ key0 = 123; key1 = 456; key2 = 678; } 2015-04-07 21:17:53.937 NSDictionary[5465:226991] dict4:{ key0 = 123; key1 = 456; key2 = 678; } 2015-04-07 21:17:53.938 NSDictionary[5465:226991] allkeys:( key1, key0, key2 ) 2015-04-07 21:17:53.938 NSDictionary[5465:226991] allvalues:( 456, 123, 678 ) 2015-04-07 21:17:53.938 NSDictionary[5465:226991] num1:456 2015-04-07 21:17:53.938 NSDictionary[5465:226991] num2:(null) 2015-04-07 21:17:53.938 NSDictionary[5465:226991] 键值对个数:3 2015-04-07 21:17:53.938 NSDictionary[5465:226991] key1=456 2015-04-07 21:17:53.938 NSDictionary[5465:226991] key0=123 2015-04-07 21:17:53.938 NSDictionary[5465:226991] key2=678
二、可变数据字典
//可变数据字典初始化
NSMutableDictionary *mDict1=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"aaa",@"name0",@"bbb",@"name1",@"ccc",@"name2", nil];
NSLog(@"%@",mDict1);
//1、加入元素到NSMutableDictionary中 (注意:当key值中存在要添加的键值对中的key值时,则添加,否则覆盖)
[mDict1 setObject:@"ddd" forKey:@"name3"];
[mDict1 setObject:@"eee" forKey:@"name1"];
NSLog(@"add: %@",mDict1);
//2、查看 中所有元素个数:
NSLog(@"%lu",mDict1.count);
NSNumber *num3 =[mDict1 objectForKey:@"name1"];
NSLog(@"%@",num3);
NSNumber *num4 = [mDict1 objectForKey:@"name10"];
NSLog(@"%@",num4);
//3、添加其他NSMutableDictionary
[mDict1 setValuesForKeysWithDictionary:dict1];
NSLog(@"++++%@",mDict1);
//4、根据key值删除
[mDict1 removeObjectForKey:@"name1"];
NSLog(@"---%@",mDict1);
//5、删除所有
[mDict1 removeAllObjects];
NSLog(@"%@",mDict1);
2015-04-07 22:12:10.717 NSDictionary[5827:248322] add: { name0 = aaa; name1 = eee; name2 = ccc; name3 = ddd; } 2015-04-07 22:12:10.718 NSDictionary[5827:248322] 4 2015-04-07 22:12:10.718 NSDictionary[5827:248322] eee 2015-04-07 22:12:10.718 NSDictionary[5827:248322] (null) 2015-04-07 22:12:10.719 NSDictionary[5827:248322] ++++{ key0 = 123; key1 = 456; key2 = 678; name0 = aaa; name1 = eee; name2 = ccc; name3 = ddd; } 2015-04-07 22:12:10.719 NSDictionary[5827:248322] ---{ key0 = 123; key1 = 456; key2 = 678; name0 = aaa; name2 = ccc; name3 = ddd; } 2015-04-07 22:12:10.719 NSDictionary[5827:248322] { }