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);
     
  • 相关阅读:
    程序编译的四个阶段
    c++的符号表的肤浅认识
    git高级用法之cheery-pick
    rust 使用国内镜像,快速安装方法
    protobuf 的enum与string转换
    c++ 获取GMT 时间和字符串
    proto3 不支持内建类型的非空判断即 hasXXX
    cmake 中的 compile_commands.json 文件
    整数划分问题(记忆化搜索和DP方法)
    查找系列合集-二分查找
  • 原文地址:https://www.cnblogs.com/kluan/p/4819418.html
Copyright © 2011-2022 走看看