OC关于NSDictionary和NSMutableDictionary方法的使用
2015-08-15 16:09
411 查看
//1.通过 值数组和键数组作为参数定义字典
NSDictionary *dict=[[NSDictionary alloc] initWithObjects:@[@"0ne",@"two"] forKeys:@[@"1",@"2"]];
NSDictionary *dicT=[NSDictionary dictionaryWithObjects:@[@"0ne",@"two"] forKeys:@[@"1",@"2"]];
//2.直接通过赋 值和键 来定义
NSDictionary *dict3=
[[NSDictionary alloc]initWithObjectsAndKeys:@"guangzhou",@"1",@"champions",@"2",@"2013",@"3",nil];
//3.前面是键,后面是值
NSDictionary *dict5=@{@"1":@"guoan",@"2":@"no.1",@"3":@"forever"};
NSLog(@"%@",dict);
NSLog(@"%@",dict3);
NSLog(@"%@",dict5);
//返回字典 值或键的个数
NSUInteger len= [dict3 count];
//通过 键找到值
NSString *str=[dict3 objectForKey:@"2"];
NSLog(@"len=%lu--%@",len,str);
//返回字典中所有的键
NSArray *arrayKey3=[dict3 allKeys];
//返回字典中所有的值
NSArray *arrayObject3=[dict3 allValues];
NSLog(@"key-%@\n object-%@",arrayKey3,arrayObject3);
//快速枚举法
for (NSString *key in [dict5 allKeys]) {
//快速枚举法只能遍历字典中的键
NSLog(@"%@",[dict5 objectForKey:key]);
//通过键再找到值
}
//可变字典
NSMutableDictionary *dictM=
[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"beijing",@"1",@"welcome",@"2",@"to",@"3",@"you",@"4", nil];
NSLog(@"dictM-%@",dictM);
//重置字典
[dictM setDictionary:dict5];
//添加键值对
[dictM setObject:@"beijing" forKey:@"qianfeng"];
NSLog(@"%@",dictM);
//删除键值对
[dictM removeObjectForKey:@"qianfeng"];
NSLog(@"%@",dictM);
NSDictionary *dict=[[NSDictionary alloc] initWithObjects:@[@"0ne",@"two"] forKeys:@[@"1",@"2"]];
NSDictionary *dicT=[NSDictionary dictionaryWithObjects:@[@"0ne",@"two"] forKeys:@[@"1",@"2"]];
//2.直接通过赋 值和键 来定义
NSDictionary *dict3=
[[NSDictionary alloc]initWithObjectsAndKeys:@"guangzhou",@"1",@"champions",@"2",@"2013",@"3",nil];
//3.前面是键,后面是值
NSDictionary *dict5=@{@"1":@"guoan",@"2":@"no.1",@"3":@"forever"};
NSLog(@"%@",dict);
NSLog(@"%@",dict3);
NSLog(@"%@",dict5);
//返回字典 值或键的个数
NSUInteger len= [dict3 count];
//通过 键找到值
NSString *str=[dict3 objectForKey:@"2"];
NSLog(@"len=%lu--%@",len,str);
//返回字典中所有的键
NSArray *arrayKey3=[dict3 allKeys];
//返回字典中所有的值
NSArray *arrayObject3=[dict3 allValues];
NSLog(@"key-%@\n object-%@",arrayKey3,arrayObject3);
//快速枚举法
for (NSString *key in [dict5 allKeys]) {
//快速枚举法只能遍历字典中的键
NSLog(@"%@",[dict5 objectForKey:key]);
//通过键再找到值
}
//可变字典
NSMutableDictionary *dictM=
[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"beijing",@"1",@"welcome",@"2",@"to",@"3",@"you",@"4", nil];
NSLog(@"dictM-%@",dictM);
//重置字典
[dictM setDictionary:dict5];
//添加键值对
[dictM setObject:@"beijing" forKey:@"qianfeng"];
NSLog(@"%@",dictM);
//删除键值对
[dictM removeObjectForKey:@"qianfeng"];
NSLog(@"%@",dictM);
相关文章推荐
- hdu 1588 Gauss Fibonacci
- 构建乘积数组
- leetcode 两个有序数组合成
- HDU 2825 Wireless Password (AC自动机 + 状态压缩DP)
- Codeforces Round #283(Div.2) A,B,C,D,E 解题报告
- NYOJ-1022合纵连横(并查集的删除操作)
- Go 函数
- [C++11 并发编程] 07 - Mutex 死锁
- SAP Down Payment的设置和使用
- java反射
- java中常用数据类型转换器
- 集合
- ....
- POJ 3253 Fence Repair
- 最大子段和
- 编写高质量代码改善C#程序的157个建议——建议36:使用FCL中的委托声明
- Openerp附件存储位置
- POJ 3349-Snowflake Snow Snowflakes(哈希)
- OC字符串总结
- apache Apache winnt_accept: Asynchronous AcceptEx failed 错误的解决