1 //创建字典 2 NSMutableDictionary *mutableDic=[[NSMutableDictionary alloc] 3 initWithObjectsAndKeys:@"jay",@"name", 4 @"22",@"age", 5 @"f",@"gender", nil]; 6 NSLog(@"%@",mutableDic); 7 //创建空的字典 8 NSMutableDictionary *mutab=[NSMutableDictionary dictionary]; 9 //将字典mutableDic复制到mutab中10 [mutab setDictionary:mutableDic];
//添加键值对的方式(向字典内添加一组键值) NSDictionary *dict1=[NSDictionary dictionaryWithObject:@"166" forKey:@"height"]; [mutableDic addEntriesFromDictionary:dict1]; NSLog(@"%@",mutableDic); //直接添加方式如果key存在时,就是修改对应的value值,如果不存在则是添加 [mutableDic setValue:@"66" forKey:@"weight"]; NSLog(@"%@",mutableDic);
1 //将字典中对应key的值删除2 [mutableDic removeObjectForKey:@"weight"];3 //删除一组key对应的value值4 [mutableDic removeObjectsForKeys:@[@"height",@"age"]];5 //删除所有value值6 [mutableDic removeAllObjects];
1 //遍历 2 //(1) 1.先找到所有key 2.计算key的个数,用来循环 3.通过key的数组找到对应key的值 (这种方法较慢) 3 NSArray *keyss=[mutableDic allKeys]; 4 NSUInteger count=[mutableDic count]; 5 for(int i=0;i