NSDictionary、NSMutableDictionary的基本用法
来源:互联网 发布:mac推出磁盘快捷键 编辑:程序博客网 时间:2024/05/18 00:01
1.不可变词典NSDictionary
字典初始化
NSNumber *numObj = [NSNumber numberWithInt:100];
以一个元素初始化
NSDictionary *dic = [NSDictionary dictionaryWithObject:numObj forKey:@"key"];
初始化两个元素
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:numObj, @"valueKey", numObj2, @"value2",nil];
初始化新字典,新字典包含otherDic
NSDictionary *dic = [NSDictionary dictionaryWithDictionary:otherDic];
以文件内容初始化字典
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
常用方法
获取字典数量
NSInteger count = [dic count];
通过key获取对应的value对象
NSObject *valueObj = [dic objectForKey:@"key"];
将字典的key转成枚举对象,用于遍历
NSEnumerator *enumerator = [dic keyEnumerator];
获取所有键的集合
NSArray *keys = [dic allKeys];
获取所有值的集合
NSArray *values = [dic allValues];
2.可变数组NSMutableDictionary
初始化一个空的可变字典
NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"key1",@"v2",@"key2",nil];
NSDictionary *dic3 = [NSDictionary dictionaryWithObject:@"v3" forKey:@"key3"];
向字典2对象中添加整个字典对象3
[dic2 addEntriesFromDictionary:dic3];
向字典2对象中最佳一个新的key3和value3
[dic2 setValue:@"value3" forKey:@"key3"];
初始化一个空的可变字典
NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
将空字典1对象内容设置与字典2对象相同
[dic1 setDictionary:dic2];
将字典中key1对应的值删除
[dic1 removeObjectForKey@"key1"];
NSArray *array = [NSArray arrayWithObjects:@"key1", nil];
根据指定的数组(key)移除字典1的内容
[dic2 removeObjectsForKeys:array];
移除字典所有对象
[dic1 removeAllObjects];
遍历字典
快速枚举
for (id key in dic){
id obj = [dic objectForKey:key];
NSLog(@"%@", obj);
}
一般枚举
NSArray *keys = [dic allKeys];
inr length = [keys count];
for (int i = 0; i < length;i++){
id key = [keys objectAtIndex:i];
id obj = [dic objectForKey:key];
NSLog(@"%@", obj);
}
通过枚举类型枚举
NSEnumerator *enumerator = [dic keyEnumerator];
id key = [enumerator nextObject];
while (key) {
id obj = [dic objectForKey:key];
NSLog(@"%@", obj);
key = [enumerator nextObject];
}
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- NSDictionary、NSMutableDictionary的基本用法
- Objective-C中NSDictionary和NSMutableDictionary的基本用法
- Objective-C 字典的基本用法(NSDictionary和NSMutableDictionary)
- NSDictionary和NSMutableDictionary的用法
- NSDictionary和NSMutableDictionary的用法
- NSDictionary和NSMutableDictionary的基本使用
- NSDictionary与NSMutableDictionary的基本使用
- SDL init failure, reason is: No available video device: On Ubuntu64
- Ubuntu14.04无法调节屏幕亮度
- 第二章作业
- IMS SIP相关的Android终端的想法----来自实验室好友王阳
- IO概述
- NSDictionary、NSMutableDictionary的基本用法
- 项目经理人际关系技能
- 猫猫学IOS(十四)UI之UITableView扩充_表格的修改_(增删移动)
- C# 值类型和引用类型
- Python2.x和3.x主要差异总结
- 用struct模块实现python socket收发自定义TCP包
- java延时设置
- struts1.3中<struts-config>报错
- linux下mysql的卸载过程