zoukankan      html  css  js  c++  java
  • NSDictionary 键值对查找

           NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys: @"1",@"a",
                               @"2",@"b", @"3",@"c", @"2",@"d", nil];
           NSLog(@"%zi",[dic1 count]); //结果:4
           NSLog(@"%@",[dic1 valueForKey:@"b"]);//根据键取得值,结果:2
           NSLog(@"%@",dic1[@"b"]);//还可以这样读取,结果:2
           NSLog(@"%@,%@",[dic1 allKeys],[dic1 allValues]);
    NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys: @"1",@"a",
                               @"2",@"b", @"3",@"c", @"2",@"d", nil];
           NSLog(@"%zi",[dic1 count]); //结果:4
           NSLog(@"%@",[dic1 valueForKey:@"b"]);//根据键取得值,结果:2
           NSLog(@"%@",dic1[@"b"]);//还可以这样读取,结果:2
           NSLog(@"%@,%@",[dic1 allKeys],[dic1 allValues]);
           
           NSLog(@"%@",[dic1 objectsForKeys:[NSArray arrayWithObjects:@"a",@"b",@"e","r", nil]notFoundMarker:@"not found"]);//后面一个参数notFoundMarker是如果找不到对应的key用
    NSMutableDictionary *dic=[NSMutableDictionary
                                     dictionaryWithObjectsAndKeys:@"1",@"a", @"2",@"b", @"3",@"c", @"2",@"d",nil];
           //[dic removeObjectForKey:@"b"];
           //NSLog(@"%@",dic);
           /*结果:
                              {
                              a = 1;
                              c = 3;
                              d = 2; }
                              */
           //[dic addEntriesFromDictionary:@{@"e":@"7",@"f":@"6"}];
           //NSLog(@"%@",dic);
           /*结果:
            {
            a = 1;
            c = 3; d = 2; e = 7; f = 6;
            } */
           //setValue:value forkey:key 如果key存在,修改value;否则添加
           [dic setValue:@"5" forKey:@"a"];
           [dic setValue:@"5" forKey:@"f"];
           NSLog(@"%@",dic);
     
  • 相关阅读:
    mysql 双主高可用配置
    lsyncd实时同步搭建指南
    tomcat优化
    nginx + tomcat + https配置
    supervisor安装文档
    移动设备的分辨率
    Python零基础入门(13)-------语句与流程控制
    Python零基础入门(12)-------文件读写
    Python零基础入门(11)-------dict 字典表
    Python零基础入门(10)------- str 字符串
  • 原文地址:https://www.cnblogs.com/kluan/p/4819418.html
Copyright © 2011-2022 走看看