#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //可变字典创建 NSMutableDictionary *mudic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"jay",@"name",@"22",@"age",@"F",@"gender", nil]; //插入 NSDictionary *dic=[NSDictionary dictionaryWithObject:@"166" forKey:@"height"]; [mudic addEntriesFromDictionary:dic]; [mudic setValue:@"66" forKey:@"weight"]; NSLog(@"%@",mudic); //创建新的字典,向空字典赋值 NSMutableDictionary *newdic=[NSMutableDictionary dictionary]; [newdic setDictionary:mudic]; NSMutableDictionary *newdic1=[NSMutableDictionary dictionary]; [newdic1 setDictionary:mudic]; NSLog(@"%@",newdic); //将字典中对应key值删除(1) [newdic removeObjectForKey:@"weight"]; NSLog(@"%@",newdic); //(2)利用数组 NSArray *keys=[NSArray arrayWithObjects:@"height",@"gender", nil]; [newdic removeObjectsForKeys:keys]; NSLog(@"%@",newdic); //全部删除 [newdic removeAllObjects]; NSLog(@"%@",newdic); //遍历 //先找到所有key值,计算key的个数,用以循环遍历条件;通过key的数组找到对应key的值 NSArray *keyss=[newdic1 allKeys]; //NSInteger count=[keyss count]; for (int i=0; i<keyss.count; i++) { id key=[keyss objectAtIndex:i];//对应位置 NSLog(@"%@",[newdic1 objectForKey:key]); } //快速枚举 for (id key in newdic1) { id obj=[newdic1 objectForKey:key]; NSLog(@"%@",obj); } //枚举对象 通过枚举对象进行枚举遍历 //1.将字典里的key转成枚举对象,用于遍历(1) NSEnumerator *keyenum=[newdic keyEnumerator]; id key; while (key=[keyenum nextObject]) {//从key中取值 id obj=[newdic objectForKey:key]; NSLog(@"%@",obj); } //(2) NSEnumerator *keyenum1=[newdic keyEnumerator]; id key1=[keyenum1 nextObject]; while (key1) {//从key中取值 id obj1=[newdic objectForKey:key]; NSLog(@"%@",obj1); key1=[keyenum1 nextObject]; } } return 0; }