NSMutableDictionary

 1 //
 2 //  main.m
 3 //  NSMutableDictionary
 4 //
 5 //  Created by dingxiaowei on 13-5-16.
 6 //  Copyright (c) 2013年 dingxiaowei. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Student.h"    //特别注意:如果从外部导入的类文件,则需要在问价里面看有没有对它进行添加到项目中  在这儿的操作是:首先点击蓝色的项目文件-》然后点击第二个Build Phases-》接着查看compile Items 看看里面是否包含外部导入进来的那个类的.m文件
11 
12 #pragma mark - 字典的创建
13 void dicCreat(){
14     Student *stu1=[Student studentWithName:@"dingxiaowei"];
15     Student *stu2=[Student studentWithName:@"hanpengyu"];
16     Student *stu3=[Student studentWithName:@"jiangke"];
17     NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:stu1,@"k1",stu2,@"k2",stu3,@"k3",nil];
18     //直接添加键值对
19     [dic setObject:@"value" forKey:@"key"];
20     //根据一个key删除一个键值对
21     [dic removeObjectForKey:@"key"];
22     //根据key查找value(如果找不到,则返回null)
23     NSLog(@"%@",[dic objectForKey:@"key"]);
24     //删除多个key对应的value
25     [dic removeObjectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2",nil]];
26     //将另外一个dictionary添加到当前dictionary中
27     NSDictionary *dicOther=[NSDictionary dictionaryWithObjectsAndKeys:@"valueOne",@"keyOne",@"valueTwo",@"keyTwo",nil];
28     [dic addEntriesFromDictionary:dicOther];
29     NSLog(@"%@",dic);
30 }
31 
32 int main(int argc, const char * argv[])
33 {
34 
35     @autoreleasepool {
36         dicCreat();
37         
38     }
39     return 0;
40 }
View Code

 

posted @ 2013-05-16 20:46  蓬莱仙羽  阅读(119)  评论(1编辑  收藏  举报