码迷,mamicode.com
首页 > 其他好文 > 详细

oc中字典的应用详解

时间:2014-07-31 23:37:40      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   文件   for   cti   


    NSDictionary *dic=[NSDictionary dictionaryWithObject:@"卢灿小样" forKey:@"lucan"];
     NSLog(@"%@",dic);
     NSLog(@"%@",[dic objectForKey:@"lucan"]);
    //输出dic键值对个数
    NSLog(@"%d",dic.count);
    
    NSDictionary *dic1=[NSDictionary dictionaryWithObject:@"卢灿实验2号" forKey:@"小样"];
    NSLog(@"%@",[dic1 objectForKey:@"小样"]);
    
    NSDictionary *dic2=@{@"first":@"2301",@"sec":@"2034"};
    NSDictionary *dic3=[NSDictionary dictionaryWithObjectsAndKeys:@"刘湘",@"name",@"小样",@"name1", nil];
    NSLog(@"  ----%@%@",dic2,dic3);
    
   //数组把vaule和key放到一个可变数组
    NSArray *values=@[@123,@668,@345];
    NSArray *key=@[@"first",@"swcond",@"third"];
    NSDictionary *dic4=[NSDictionary dictionaryWithObject:values forKey:key];
    NSLog(@"xxxxxxxxxxxx%@",dic4);
    
    //排序
    NSArray *arry=[dic4 keysSortedByValueUsingSelector:@selector(compare:)];
    NSLog(@"+++++++%@",arry);

    //用一个现有字典对象初始化另一个新字典对象(创建可变对象)
    NSDictionary *arry1=[[NSDictionary alloc]initWithDictionary:dic4 ];
    NSLog(@"ooooooo%@",arry1);
    
    //保存对象到内容文件
    NSString *path=@"/Users/apple/Desktop/test.plist";
    [dic2 writeToFile:path atomically:YES];
    
    //从以前保存的文件读取到字典对象
    NSDictionary *data=[NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"xxxxx%@",data);
    
    //字典的遍历key1相当于a[i]中的i,dic4就自己定义的字典
    for (id key1 in dic4) {
        id vaule=[dic4 objectForKey:key1];
        NSLog(@"qqqqqq%@%@",key,vaule);
    }
    //- (void)removeObjectForKey:(id)aKey;
    //删除键值对
    NSMutableDictionary  *dic7=[NSMutableDictionary dictionaryWithDictionary:dic2];
    [dic7 removeObjectForKey:@"sec"];
    NSLog(@"%@",dic7);
    
   // 判断key值有就替换没有就添加
    [dic7 setObject:@"3412" forKey:@"sec"];
    NSLog(@"%@",dic7);
    
    //增加dic4  字典无顺序
    [dic7 addEntriesFromDictionary:dic4];
    NSLog(@"%@",dic7);
    

 

oc中字典的应用详解,布布扣,bubuko.com

oc中字典的应用详解

标签:des   style   blog   color   io   文件   for   cti   

原文地址:http://www.cnblogs.com/liuxiang520/p/3883663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!