<span style="font-size:18px;">/*字典存在的价值 :
0. 字典类是用于保存具有映射关系(key - value对)的数据, 字典一旦创建,键值对就不可更改, 不可添加, 不可删除.仅能读取key 或者 value
1. 大容器,用来存储多个数据,
2.用来存储的数据具有--对应的关系.(使用key 来标识 value).
3.对于字典中的一对键值对(key - value)叫做字典中的一个元素. 也叫一个条目, 只要是对象就可以, 不限制类型.
4.字典是无序的
5.字典中的key是唯一的, 一个key 只能对应一个value ,一个value 可以对应多个 key.*/
//1.创建字典对象
//(1)便利构造器
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"Summer", @"name", @"male", @"gender", @20, @"height", @12, @"age", nil];
NSLog(@"%@", dic);
//(2)初始化方法
NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Summer", @"name", @"male", @"gender", @20, @"height", @12, @"age", nil];
NSLog(@"%@", dic1);
//2.求字典元素个数
NSInteger count = [dic count];
NSLog(@"count = %ld", count);
//3.获取所有的key
NSArray *keys = [dic allKeys];
NSLog(@"%@", keys);
//4获取所有的value
NSArray *value = [dic allValues];
NSLog(@"%@", value);
//5.根据key获取对应的value
//通过为对象字典发送objeForKey消息可以根据给定的key去获取对应的value,这里如果给定的key不存在,则返回null作为value,表示没有找到对应的value.
NSString *name = [dic objectForKey:@"name"];
NSLog(@"name = %@", name);
NSNumber *age = [dic objectForKey:@"age1"];
NSLog(@"age = %@", age);
NSString *gender = [dic objectForKey:@"gender"];
NSLog(@"gender = %@", gender);
NSNumber *height = [dic objectForKey:@"height"];
NSLog(@"height = %@", height);
//字典快速遍历
//快速遍历: 对于数组来说,遍历得到的是数组中的每一个元素
//对于字典来说, 遍历得到的是字典中的key, 然后通过key 获取 value
for (NSString *key in dic) {
//检索到key之后,通过objectForKey 获取key对于的value
NSLog(@"%@", [dic objectForKey:key]);
}
//对于可变字典来说,比不可变字典多了增加, 删除, 修改操作
//1.创建对象
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Summer", @"name1", @"Sunshine", @"name2", @"Angle", @"name3", nil];
NSLog(@"%@", mutableDic);
//2.求字典元素个数
NSInteger count = [mutableDic count];
NSLog(@"count = %ld", count);
//3.删除元素
[mutableDic removeObjectForKey:@"name3"];
NSLog(@"%@", mutableDic);
[mutableDic removeAllObjects];
NSLog(@"%@", mutableDic);
//批量删除
[mutableDic removeObjectsForKeys:[NSArray arrayWithObjects:@"name1", @"name2", nil]];
NSLog(@"%@", mutableDic);
//4.添加元素
[mutableDic setObject:@"Kimi" forKey:@"name1"];
NSLog(@"%@", mutableDic);
//5.修改key对应的value
[mutableDic setObject:@"Angle" forKey:@"name1"];
NSLog(@"%@", mutableDic);
//setObject: forKey: 工作原理
// 先根据指定得key查找字典中是否存在相同的key.(查找字典中是否存在指定的key).如果存在则修改key对应的value. 如果不存在,在字典中添加一个元素,key - value 键值对</span>
<span style="font-size:18px;">
</span>
<pre name="code" class="objc"><span style="font-size:18px;">//集合
//1.集合与数学集合一样, 存储的元素互不相同(互异性)(唯一性).
//2.存储的元素无序性
//3.存储元素必须是对象类型
//1.创建集合对象
//(1)遍历构造器方法
NSSet *set1 = [NSSet setWithObjects:@"Summer", @"Sunshine", @"Angle", @"procedure", @"oriented", @"programming", nil];
NSLog(@"%@", set1);
//(2)初始化方法
NSSet *set2 = [[NSSet alloc] initWithObjects:@"Summer", @"Sunshine", @"Angle", @"procedure", @"oriented", @"programming", @"Object", @"oriented", @"programming", nil];
NSLog(@"%@", set2);
//2.获取元素个数
NSInteger count = [set2 count];
NSLog(@"count = %ld", count);
//3.获取集合中的某个元素(为了方便性)
NSString *str1 = [set2 anyObject];
NSLog(@"%@", str1);
NSString *str2 = [set2 anyObject];
NSLog(@"%@", str2);
NSString *str3 = [set2 anyObject];
NSLog(@"%@", str3);
NSString *str4 = [set2 anyObject];
NSLog(@"%@", str4);
NSString *str5 = [set2 anyObject];
NSLog(@"%@", str5);
//4.判断集合中是否包含某个对象
BOOL isExist = [set2 containsObject:@"Summer"];
NSLog(@"%d", isExist);
</span>
<span style="font-size:18px;">//可变集合
NSMutableSet *set2 = [[NSMutableSet alloc] initWithObjects:@"Summer", @"Sunshine", @"Angle", @"procedure", @"oriented", @"programming", @"Object", @1, @"programming", @"programming", @"programming", @1, @1,nil];
NSLog(@"%@", set2);
//1.添加元素
[set2 addObject:@"Cindy"];
NSLog(@"%@", set2);
//2.移除元素
[set2 removeObject:@"Summer"];
NSLog(@"%@", set2);
//求带有记录对象重复次数的子类NSCountSet
NSCountedSet *countedSet = [NSCountedSet setWithObjects:@"programming", @"programming",@"programming",nil];
//通过实例方法countForObject:来得到指定对象的重复次数
NSInteger count = [countedSet countForObject:@"programming"];
NSLog(@"count = %ld", count);
</span>