1 dictionary
/*
字典
存储的内存不是连续的
用key和value进行对应
kvc 键值编码
**/
//定义一个最简单的字典
NSDictionary *dict1 =[NSDictionary dictionaryWithObject:@"1" forKey:@"a"];
NSLog(@"dict1---> %@",dict1);
//定义一个数组字典
NSDictionary *dict2 =[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1",@"2",@"3",nil] forKeys:[NSArray arrayWithObjects:@"a",@"b",@"c", nil]];
NSLog(@"dict2--->%@",dict2);
//定义一个字典
NSDictionary *dict3=@{
@"1":@"a",
@"2":@"b",
@"3":@"d"
};
NSLog(@"dict3----->%@",dict3);
//字典的长度
int count =(int)[dict2 count];
NSLog(@"count--->%d",count);
//根据字典的key获取值
NSString*value = [dict2 valueForKey:@"b" ];
NSLog(@"value---%@",value);
//根据字典的key获取值value
NSString *value2= [dict2 objectForKey:@"b"];
NSLog(@"value2---%@",value2);
//得到字典中所有的值(数组)
NSArray *allvalues =[dict2 allValues];
NSLog(@"allvalues--->%@",allvalues);
//得到字典中所有的key(数组)
NSArray *allkys = [dict2 allKeys];
NSLog(@"allkys--->%@",allkys);
//用所有的key找到所有的值 (返回值array)
NSArray *array = [dict2 objectsForKeys:[NSArray arrayWithObjects:@"a",@"b",@"d", nil] notFoundMarker:@"not found"];
NSLog(@"str--->%@",array);
//遍历字典
for(NSString *key in dict2){
NSLog(@"key-->%@ value--->%@",key,[dict2 objectForKey:key]);
}
NSEnumerator *en =[dict2 keyEnumerator];
id key =nil;
while (key=[en nextObject]) {
NSLog(@"key --%@",key);
}
[dict2 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
}];