• OC语言基础之NSDictionary


    1.NSDictionary字典的创建

       1:  // key value
       2:      // key -==> value
       3:      NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"key"];
       4:      
       5:      NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", nil];
       6:      NSArray *values = [NSArray arrayWithObjects:@"v1", @"v2", nil];
       7:      NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:values
       8:                                                        forKeys:keys];
       9:      NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"v", @"key",
      10:                             @"v1", @"key1",
      11:                             nil];
      12:      NSLog(@"%@", dict2);
      13:      
      14:      
      15:      //    BOOL isSuccess = [dict writeToFile:@"/Users/apple/Desktop/1.txt"
      16:      //           atomically:YES];
      17:      //    if (isSuccess) {
      18:      //        NSLog(@"成功");
      19:      //    }else{
      20:      //        NSLog(@"写入失败");
      21:      //    }
      22:      
      23:      NSDictionary *dict3 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/apple/Desktop/1.txt"];
      24:      NSLog(@"%@", dict3);

    字典的遍历

       1:   NSDictionary *pDict = [NSDictionary dictionaryWithObjectsAndKeys:@"abc", @"name",
       2:                             @"11", @"age",
       3:                             @"男", @"gender",
       4:                             nil];
       5:      //    NSArray *keys = [pDict allKeys];
       6:      //    for (int i = 0; i<[pDict count]; i++) {
       7:      //        NSString *key = [keys objectAtIndex:i];
       8:      //        NSString *value = [pDict objectForKey:key];
       9:      //        NSLog(@"%@: value= %@", key, value);
      10:      //    }
      11:      
      12:      //    for (id key in pDict) {
      13:      //        NSLog(@"%@", key);
      14:      //    }
      15:      //
      16:      
      17:      [pDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
      18:          NSLog(@"%@", key);
      19:          
      20:          // 修改stop指向内存的值
      21:          *stop = YES;
      22:      }];

    2.可变字典

       1:   
       2:      NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v", @"key",
       3:                                    nil];
       4:      [dictM setObject:@"1" forKey:@"key"];
       5:      NSLog(@"%@", dictM);

    3.具体使用的例子

       1:  // 名字
       2:      // 年龄
       3:      // 性别
       4:      NSDictionary *pDict = [NSDictionary dictionaryWithObjectsAndKeys:
       5:                             @"11", @"age",
       6:                             @"abc", @"name",
       7:                             @"男", @"gender",
       8:                             nil];
       9:      
      10:      
      11:      //    NSMutableArray *array = [NSMutableArray array];
      12:      //    [array addObject:pDict];
      13:      
      14:      id name = [pDict objectForKey:@"name"];
      15:      NSLog(@"%@", name);

    4.各常用类对比

       1:  /*
       2:   集合
       3:   1.NSArrayNSMutableArray
       4:   * 有序
       5:   * 快速创建(不可变):@[obj1, obj2, obj3]
       6:   * 快速访问元素:数组名[i]
       7:  
       8:   2.NSSetNSMutableSet
       9:   * 无序
      10:  
      11:   3.NSDictionaryNSMutableDictionary
      12:   * 无序
      13:   * 快速创建(不可变):@{key1 : value1,  key2 : value2}
      14:   * 快速访问元素:字典名[key]
      15:   */

    例子

       1:   /*
       2:       字典:
       3:  
       4:       key ----> value
       5:       索引 ----> 文字内容
       6:  
       7:       里面存储的东西都是键值对
       8:  
       9:  
      10:       */
      11:      
      12:      // NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"name"];
      13:      
      14:      
      15:      // NSArray *keys = @[@"name", @"address"];
      16:      // NSArray *objects = @[@"jack", @"北京"];
      17:      
      18:      // NSDictionary *dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
      19:      
      20:      
      21:      /*
      22:       NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
      23:       @"jack", @"name",
      24:       @"北京", @"address",
      25:       @"32423434", @"qq", nil];*/
      26:      
      27:      
      28:      NSDictionary *dict = @{@"name" : @"jack", @"address" : @"北京"};
      29:      
      30:      // id obj = [dict objectForKey:@"name"];
      31:      
      32:      id obj = dict[@"name"];
      33:      
      34:      NSLog(@"%@", obj);
      35:      
      36:      
      37:      
      38:      // 返回的是键值对的个数
      39:      NSLog(@"%ld", dict.count);
  • 相关阅读:
    Kafka发送和接收消息
    Kafka主题topic的增删改查
    Kafka启动
    synchronize底层实现原理
    StringBuffer和StringBuilder的区别
    常见的排序算法
    Redis缓存穿透,缓存击穿,缓存雪崩原理及解决方案
    Redis持久化策略
    redis支持的数据类型
    Bytes 类型
  • 原文地址:https://www.cnblogs.com/zeyang/p/4318998.html
Copyright © 2020-2023  润新知