字典用于保存具有映射关系数据的集合
一个key—value对认为是一个条目(entry),字典是存储key—value对的容器
与数组不同,字典靠key存取元素
key不能重复,value必须是对象
键值对在字典中是无序存储的
字典分:不可变字典(NSDictionary)和可变字典(NSMutableDictionary)
不可变字典一旦创建,键值对就不可更改,不可添加,不可删除,仅能读取key或者value
常用方法有:
1、创建字典对象
2、获取所有key值,获取所有value值
3、通过key值查询value
1、常见字典的常用方法
在创建字典对象时需要赋值键值对,但是顺序为:值,键,(值在前键在后的形式)
NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];
NSLog(@"%@",dic1);
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];
NSLog(@"%@",dic2);
NSArray *keys = @[@"name",@"age",@"gender"];
NSLog(@"%@",dic1);
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];
NSLog(@"%@",dic2);
NSArray *keys = @[@"name",@"age",@"gender"];
NSArray *values = @[@"Duke",@33,@"male"];
字典的语法糖形式
键值对之间以逗号隔开,键和值之间以冒号隔开
NSDictionary *dic5 = @{@"name" : @"Duke",@"age" : @33,@"gender" : @"male"};
NSLog(@"%@",dic5);
创建字典对象时两个数组元素个数必须一致
NSDictionary *dic3 = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
NSLog(@"%@",dic3);
NSDictionary *dic4 = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(@"%@",dic3);
NSDictionary *dic4 = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(@"%@",dic4);
通过count方法获取字典中键值对的个数
NSInteger count = [dic4 count];
NSLog(@"%ld",count);
获取字典中所有的键
NSArray *allKeys = [dic4 allKeys];
NSLog(@"%@",allKeys);
获取字典中所有的值组成的值
NSArray *allValues = [dic4 allValues];
NSLog(@"%@",allValues);
通过指定的键获取其在字典中对应的值
id object = [dic4 objectForKey:@"age"];
NSLog(@"%@",object);
for (int i = 0; i < count; i++) {
id key = [allKeys objectAtIndex:i];
NSLog(@"%@",object);
for (int i = 0; i < count; i++) {
id key = [allKeys objectAtIndex:i];
根据当前遍历得到的key去获取对应的value
id value = [dic4 objectForKey:key];
NSString *result = [value isKindOfClass:[NSString class]] ? @"YES" : @"NO";
NSLog(@"%@:%@-->%@",key,value,result);
NSString *result = [value isKindOfClass:[NSString class]] ? @"YES" : @"NO";
NSLog(@"%@:%@-->%@",key,value,result);
}
- 字典初始化
void arrayTest5()
{
//字典初始化方式1
NSDictionary *dictionary1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value0",@"key0",
@"value1",@"key1",
@"value2",@"key2",
@"value3",@"key3",
@"value4",@"key4",
nil];
NSLog(@"%@",dictionary1);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
}
*/
//字典初始化方式2
NSArray *valueArray = @[@"value0",@"value1",@"value2",@"value3",@"value4"];
NSArray *keyArray = @[@"key0",@"key1",@"key2",@"key3",@"key4"];
NSDictionary *dictionary2 = [[NSDictionary alloc]initWithObjects:valueArray forKeys:keyArray];
NSLog(@"%@",dictionary2);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
}
*/
//字典初始化方式3
NSDictionary *dictionary3 = @{
@"key0":@"value0",
@"key1":@"value1",
@"key2":@"value2",
@"key3":@"value3",
@"key4":@"value4"
};
NSLog(@"%@",dictionary3);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
}
*/
//调用NSDictionary静态方法生成字典
NSDictionary *dictionary4 = [NSDictionary dictionaryWithObject:@"value0" forKey:@"key0"];
NSLog(@"%@",dictionary4);
/*
{
key0 = value0;
}
*/
NSDictionary *dictionary5 = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
NSLog(@"%@",dictionary5);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
}
*/
NSDictionary *dictionary6 = [NSDictionary dictionaryWithObjectsAndKeys:@"value0",@"key0",@"value1",@"key1",@"value2",@"key2",@"value3",@"key3",@"value4",@"key4", nil];
NSLog(@"%@",dictionary6);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
}
*/
}
- 字典基本常用用法
void arrayTest6()
{
NSDictionary *dictionary1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value0",@"key0",
@"value1",@"key1",
@"value2",@"key2",
@"value3",@"key3",
@"value4",@"key4",
@"value4",@"key5",
nil];
NSLog(@"%@",dictionary1);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
key5 = value4
}
*/
NSLog(@"%ld",dictionary1.count);
/* 5 */
//输出特定元素
NSLog(@"%@",dictionary1[@"key0"]);
NSLog(@"%@",[dictionary1 objectForKey:@"key0"]);
/* value0 */
//获取所有key
NSLog(@"%@",dictionary1.allKeys);
/*
(
key3,
key1,
key4,
key2,
key0
)
*/
//获取所有value
NSLog(@"%@",dictionary1.allValues);
/*
(
value3,
value1,
value4,
value2,
value0
)
*/
//获取特定value对应的所有key
NSLog(@"%@",[dictionary1 allKeysForObject:@"value4"]);
/*
(
key4,
key5
)
*/
//分别获取key对应的value,对于不存在的key,返回"not found"
NSLog(@"%@",[dictionary1 objectsForKeys:@[@"key0",@"key1",@"key8"] notFoundMarker:@"not found"]);
/*
(
value0,
value1,
"not found"
)
*/
NSDictionary *dictionaryOther = @{@"key0":@"value0"};
BOOL result = [dictionary1 isEqualToDictionary:dictionaryOther];
NSLog(@"%hhd",result);
/* 0 */
//保存到本地文件
[dictionary1 writeToFile:@"/Users/new/Desktop/dictionary.txt" atomically:YES];
/*
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key0</key>
<string>value0</string>
<key>key1</key>
<string>value1</string>
<key>key2</key>
<string>value2</string>
<key>key3</key>
<string>value3</string>
<key>key4</key>
<string>value4</string>
<key>key5</key>
<string>value4</string>
</dict>
</plist>
*/
//读取本地文件,反序列化为字典
NSDictionary* dictionary2 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/new/Desktop/dictionary.txt"];
NSLog(@"%@",dictionary2);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
key5 = value4;
}
*/
//修改字典元素值,如果key存在,则修改对应的value,如果key不存在,则插入一个新元素
[dictionary2 setValue:@"update value0" forKey:@"key0"];
NSLog(@"%@",dictionary2);
/*
{
key0 = "update value0";
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
key5 = value4;
}
*/
//插入一个新元素,该key不存在
[dictionary2 setValue:@"key not found" forKey:@"noKey"];
NSLog(@"%@",dictionary2);
/*
{
key0 = "update value0";
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
key5 = value4;
noKey = "key not found";
}
*/
}
- NSDictionary遍历
void dictionaryTest()
{
NSDictionary *dictionary1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"value0",@"key0",
@"value1",@"key1",
@"value2",@"key2",
@"value3",@"key3",
@"value4",@"key4",
@"value4",@"key5",
nil];
//遍历方法一
for (NSString *key in dictionary1) {
NSLog(@"%@ = %@",key,dictionary1[key]);
}
/*
key3 = value3
key1 = value1
key4 = value4
key2 = value2
key0 = value0
key5 = value5
*/
//遍历方法二
//NSEnumerator *objectEnumerator = [dictionary1 objectEnumerator]; //objectEnumerator获取value枚举
NSEnumerator *keyEumerator = [dictionary1 keyEnumerator]; //keyEnumerator获取key枚举
NSString *key = nil;
while (key = [keyEumerator nextObject]) {
NSLog(@"%@ = %@",key,dictionary1[key]);
}
/*
key3 = value3
key1 = value1
key4 = value4
key2 = value2
key0 = value0
key5 = value5
*/
//遍历方法三
[dictionary1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@ = %@",key,obj);
}];
/*
key3 = value3
key1 = value1
key4 = value4
key2 = value2
key0 = value0
key5 = value5
*/
}
-
NSMutableDictionary
NSMutableDictionary继承于NSDictionary,在此基础上新增加对元素新增、删除、更新等操作,如下代码演示
void mutableDictionaryTest()
{
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc]initWithCapacity:7];
NSLog(@"%ld",mutableDictionary.count);
/* 0 */
[mutableDictionary setValue:@"value0" forKey:@"key0"];
[mutableDictionary setValue:@"value1" forKey:@"key1"];
[mutableDictionary setValue:@"value2" forKey:@"key2"];
[mutableDictionary setValue:@"value3" forKey:@"key3"];
[mutableDictionary setValue:@"value4" forKey:@"key4"];
[mutableDictionary setValue:@"value5" forKey:@"key5"];
[mutableDictionary setValue:@"value6" forKey:@"key6"];
[mutableDictionary setValue:@"value7" forKey:@"key7"];
NSLog(@"%ld",mutableDictionary.count);
/* 8 */
NSLog(@"%@",mutableDictionary);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
key5 = value5;
key6 = value6;
key7 = value7;
key8 = value8;
}
*/
//删除元素
[mutableDictionary removeObjectForKey:@"key7"];
NSLog(@"%ld",mutableDictionary.count);
/* 7 */
NSLog(@"%@",mutableDictionary);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
key5 = value5;
key6 = value6;
}
*/
//对于key存在,则修改对应的value,否则添加新元素
[mutableDictionary setValue:@"update value6" forKey:@"key6"];
NSLog(@"%@",mutableDictionary);
//移除多个key对应的元素
[mutableDictionary removeObjectsForKeys:@[@"key6",@"key5"]];
NSLog(@"%@",mutableDictionary);
/*
{
key0 = value0;
key1 = value1;
key2 = value2;
key3 = value3;
key4 = value4;
}
*/
//移除所有元素
[mutableDictionary removeAllObjects];
NSLog(@"%ld",mutableDictionary.count);
/* 0 */
}

浙公网安备 33010602011771号