zoukankan      html  css  js  c++  java
  • oc中字典的应用详解

    
        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);
        
  • 相关阅读:
    bzoj 1208: [HNOI2004]宠物收养所
    bzoj 1207: [HNOI2004]打鼹鼠
    【NOIP模拟赛】小奇的矩阵
    【NOIP模拟赛】小奇挖矿 2
    Making the Grade POJ
    POJ 3616Milking Time
    [USACO08JAN]电话线Telephone Lines
    Radar Installation POJ
    Warfare And Logistics UVA
    【NOIP2009】最优贸易
  • 原文地址:https://www.cnblogs.com/liuxiang520/p/3883663.html
Copyright © 2011-2022 走看看