------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
1. NSMutableDictionary介绍
NSMutableDictionary是NSDictionary的子类,可以使用NSDictionary的所有方法,是可变字典。
2. NSMutableDictionary的创建
<span style="white-space:pre"> </span>//创建空字典
NSMutableDictionary *dict1=[NSMutableDictionary dictionary];
//创建指定长度的字典
NSMutableDictionary *dict2=[NSMutableDictionary dictionaryWithCapacity:10];
3. NSMutableDictionary的基本用法
<span style="white-space:pre"> </span>//给可变字典添加键值对
[dict1 setValue:@"zhangsan" forKey:@"zs"];
[dict1 setValue:@"lisi" forKey:@"ls"];
//修改字典中指定元素
[dict1 setObject:@"zhangsanfeng" forKey:@"zs"];
//用简写形式修改字典中指定元素
dict1[@"zs"]=@"zhangsanfeng";
//查找字典中的元素
//获取字典中的所有key值
NSArray * arr=[dict1 allKeys];
//在数组中查找是否有存在某个key
if ([arr containsObject:@"ls"]) {
NSLog(@"The key of 'ls' exists!");
}
//删除字典中指定元素
[dict1 removeObjectForKey:@"zs"];
//删除字典中所有元素
[dict1 removeAllObjects];