NSDictionary /NSMutableDictionary
1.概念 : 字典是关键字 ‘key’ 及其定义的值 ‘value’所构成的集合,即它是由key-value组成的键值对的集合。通常来讲,’key’一般是’NSString’类型,’value’是OC中任意的对象。对于字典来讲,我们就是通过’key’来访问’value’.
2.为什么在有了数组后,还需要字典呢?在由大量的数据或频繁的查找信息,使用字典效率更高。因为字典是采用键查询的优化存储方法。
3.在OC中我们使用’NSDictionary’ 和 ‘NSMutableDictionary’ 来表示字典。其中’NSMutableDictionary’是’NSDictionary’的子类。
4.字典初始化
//数组的遍历
//快速枚举
NSMutableArray *marray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
for (NSString *element in marray)
{
NSLog(@"element = %@",element);
}
//使用系统提供的枚举类 NSEnumerator
NSEnumerator *enumerator = [marray objectEnumerator];
NSString *ele ;
while (ele = [enumerator nextObject])
{
NSLog(@"%@",ele);
}
//当我们在遍历可变数组时不可修改数组
/*
字典
*/
//初始化
//创建一个空字典
NSDictionary *dictionary1 = [[NSDictionary alloc]init];
NSDictionary *dictionary2 = [NSDictionary dictionary];
//创建一个键值队
NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"rick" forKey:@"name"];
//创建多个键值队
NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjectsAndKeys:@"rick",@"name",@23,@"key",@"guangzhou",@"name",nil];
//value在前,key在后,value 和 key 要成双出现
NSLog(@"%@",dictionary4);
//根据一个字典创建一个字典
NSDictionary *dictionary5 = [NSDictionary dictionaryWithDictionary:dictionary4];
NSLog(@"%@",dictionary5);
//新语法创建字典
NSDictionary *dictionary6 = @{};
NSDictionary *dictionary7 = @{@"name":@"rick"};
NSDictionary *dictionary8 = @{@"age":@"rick" , @"age":@25 ,@"address":@"guangzhou"};
NSDictionary *dictionary11 = [NSDictionary dictionaryWithContentsOfFile:@""];
NSDictionary *dictionary12 = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@""]];
5.字典中键值队的数量
NSUInteger count = dictionary4.count ;
6.如何访问字典
//通过key访问字典中对应的值
NSString *name = [dictionary4 objectForKey:@"name"];
//新语法
NSString *newName = dictionary4[@"name"];
/如何遍历字典中的所有值
//表示字典中所有的key
NSArray *keysArray = dictionary4.allKeys ;
//表示字典中所有的value
NSArray *valuesArray = dictionary4.allValues;
for (NSString *key in keysArray)
{
NSLog(@"%@:%@",key,dictionary4[key]);
// [dictionary4 objectForKey:key]
}
NSLog(@"\n");
enumerator = dictionary8.keyEnumerator ;
NSString *key;
while (key = [enumerator nextObject])
{
NSLog(@"%@:%@",key,dictionary8[key]);
}
7.在字典中不能够存在相同key,如果相同的key,则会替换
8.在字典中键值是无序的
NSMutableDictionary
1.初始化
//1.初始化方法
NSMutableDictionary *mdictionary1= [NSMutableDictionary dictionary];
NSMutableDictionary *mdictionary2 = [NSMutableDictionary dictionaryWithObject:@"riri" forKey:@"name"];
NSMutableDictionary *mdictionary3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"rici",@"name",@"25",@"age",@"guangzhou",@"address", nil];
//没有新语法
NSDictionary *mddictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary8];
//自己独有的初始化方法
NSMutableDictionary *mdictionary5 = [NSMutableDictionary dictionaryWithCapacity:10];
NSMutableDictionary *mdictionary8 = [NSMutableDictionary dictionaryWithContentsOfFile:@""];
NSMutableDictionary *mdictionary9 = [NSMutableDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@""]];
//增
[mdictionary5 setValue:@"rick" forKey:@"name"];
[mdictionary5 setValue:@25 forKey:@"age"];
NSLog(@"mdictionary5 = %@",mdictionary5);
//改
[mdictionary5 setValue:@"herry" forKey:@"name"];
NSLog(@"mdictionary5 = %@",mdictionary5);
//
mdictionary5[@"name"] = @"zhou";
NSLog(@"mdictionary5 = %@",mdictionary5);
//删
[mdictionary5 removeObjectForKey:@"name"];
[mdictionary5 removeObjectForKey:@[]];
[mdictionary5 removeAllObjects];