• Dictionary的用法


     1 atomically:原子性,当文件内容写入成功之后再创建文件。
     2 
     3 NSArray *keys = [NSArray arrayWithObjects:@"name", @"age", nil];
     4 NSArray *values = [NSArray arrayWithObjects:@"wzx", [NSNumber numberWithInt:16], nil];
     5 NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
     6 
     7     // NSDictionary是不可变的
     8    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];
     9     
    10     // 最常用的初始化方式
    11    dict = [NSDictionary dictionaryWithObjectsAndKeys:
    12             @"v1", @"k1",
    13             @"v2", @"k2",
    14             @"v3", @"k3", nil];
    15     
    16     NSArray *objects = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
    17     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
    18     dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    19     NSLog(@"%@", dict);
    20 
    21 
    22     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    23             @"v1", @"k1",
    24             @"v2", @"k2",
    25             @"v3", @"k3", nil];
    26     
    27     // count是计算有多少个键值对(key-value)
    28     NSLog(@"count=%zi", dict.count);
    29     
    30     // 由于NSDictionary是不可变的,所以只能取值,而不能修改值
    31    id obj = [dict objectForKey:@"k2"];
    32     NSLog(@"obj=%@", obj);
    33 
    34     // 将字典写入文件中
    35    NSString *path = @"/Users/apple/Desktop/dict.xml";
    36     [dict writeToFile:path atomically:YES];
    37     
    38     // 从文件中读取内容
    39    dict = [NSDictionary dictionaryWithContentsOfFile:path];
    40     NSLog(@"dict=%@", dict);
    41 
    42 
    43    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    44                           @"v1", @"k1",
    45                           @"v2", @"k2",
    46                           @"v3", @"k3", nil];
    47     // 返回所有的key
    48     NSArray *keys = [dict allKeys];
    49     //NSLog(@"keys=%@", keys);
    50     
    51     NSArray *objects = [dict allValues];
    52     //NSLog(@"objects=%@", objects);
    53     
    54     // 根据多个key取出对应的多个value
    55     // 当key找不到对应的value时,用marker参数值代替
    56     objects = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1", @"k2", @"k4", nil] notFoundMarker:@"not-found"];
    57     NSLog(@"objects=%@", objects);
    58 
    59 
    60   #pragma mark 遍历字典
    61   NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    62                           @"v1", @"k1",
    63                           @"v2", @"k2",
    64                           @"v3", @"k3", nil];
    65     // 遍历字典的所有key
    66     for (id key in dict) {
    67         id value = [dict objectForKey:key];
    68         NSLog(@"%@=%@", key, value);
    69     }
    70 
    71 
    72  NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    73                           @"v1", @"k1",
    74                           @"v2", @"k2",
    75                           @"v3", @"k3", nil];
    76     // key迭代器
    77     NSEnumerator *enumer = [dict keyEnumerator];
    78     id key = nil;
    79     while ( key = [enumer nextObject]) {
    80         id value = [dict objectForKey:key];
    81         NSLog(@"%@=%@", key, value);
    82     }
    83     
    84       // 对象迭代器
    85     // [dict objectEnumerator];
    86 
    87 
    88 
    89 //block
    90  NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    91                           @"v1", @"k1",
    92                           @"v2", @"k2",
    93                           @"v3", @"k3", nil];
    94     [dict enumerateKeysAndObjectsUsingBlock:
    95      ^(id key, id obj, BOOL *stop) {
    96         NSLog(@"%@=%@", key, obj);
    97     }];
    98  
  • 相关阅读:
    RHEL虚机分区在线扩容操作指导说明
    Kafka vs AMQ技术比对
    数据库选型之MySQL vs ElasticSearch
    JAVA ElasticSearch 访问控制最佳解决方案
    EFS解密----未重装系统
    EFS加密解密----重装系统后
    EXCEL表格单元格中包含数字英文和汉字,如何自动去掉汉字,保留英文和数字
    excel如何用公式判断单元格的值是否为数字、英文、中文,以及相应的计数
    EXCEL计算数字、汉字、英文单元格的计数
    sql基本操作
  • 原文地址:https://www.cnblogs.com/wangshengl9263/p/3076714.html
Copyright © 2020-2023  润新知