• NSDictionary使用小结(字典)


    1. #import <Foundation/Foundation.h>  
    2.   
    3.   
    4. int main(int argc, const char * argv[])  
    5. {  
    6.   
    7.     @autoreleasepool {  
    8.   
    9.         //创建字典  
    10.         NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];  
    11.         NSLog(@"dic1 :%@", dic1);  
    12.           
    13.           
    14.         //创建多个字典  
    15.         NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:  
    16.                               @"value1", @"key1",  
    17.                               @"value2", @"key2",  
    18.                               @"value3", @"key3",  
    19.                               @"value4", @"key4",  
    20.                               nil];  
    21.         NSLog(@"dic2 :%@", dic2);  
    22.           
    23.           
    24.         //根据现有的字典创建字典  
    25.         NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];  
    26.         NSLog(@"dic3 :%@", dic3);  
    27.           
    28.           
    29.         //根据key获取value  
    30.         NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);  
    31.           
    32.         //获取字典数量  
    33.         NSLog(@"dic count :%d", dic3.count);  
    34.           
    35.         //所有的键集合  
    36.         NSArray *keys = [dic3 allKeys];  
    37.         NSLog(@"keys :%@", keys);  
    38.           
    39.         //所有值集合  
    40.         NSArray *values = [dic3 allValues];  
    41.         NSLog(@"values :%@", values);  
    42.           
    43.           
    44.           
    45.         NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
    46.                                            @"mvalue1", @"mkey1",  
    47.                                            @"mvalue2", @"mkey2", nil];  
    48.         //添加现有的字典数据  
    49.         [mutableDic addEntriesFromDictionary:dic3];  
    50.         NSLog(@"mutableDic :%@",mutableDic);  
    51.           
    52.         //添加新的键值对象  
    53.         [mutableDic setValue:@"set1" forKey:@"setKey1"];  
    54.         NSLog(@"set value for key :%@",mutableDic);  
    55.           
    56.         //以新的字典数据覆盖旧的字典数据  
    57.         [mutableDic setDictionary:dic2];  
    58.         NSLog(@" set dictionary :%@",mutableDic);  
    59.           
    60.         //根据key删除value  
    61.         [mutableDic removeObjectForKey:@"key1"];  
    62.         NSLog(@"removeForkey :%@",mutableDic);  
    63.           
    64.         //快速遍历  
    65.         for(id key in mutableDic) {  
    66.             NSLog(@"key :%@  value :%@", key, [mutableDic objectForKey:key]);  
    67.         }  
    68.           
    69.         //枚举遍历  
    70.         NSEnumerator *enumerator = [mutableDic keyEnumerator];  
    71.         id key = [enumerator nextObject];  
    72.         while (key) {  
    73.             NSLog(@"enumerator :%@", [mutableDic objectForKey:key]);  
    74.             key = [enumerator nextObject];  
    75.         }  
    76.           
    77.           
    78.         //根据key数组删除元素  
    79.         [mutableDic removeObjectsForKeys:keys];  
    80.         NSLog(@"removeObjectsForKeys :%@",mutableDic);  
    81.           
    82.         [mutableDic removeAllObjects];  
    83.         //删除所有元素  
    84.         NSLog(@"remove all :%@", mutableDic);  
    85.     }  
    86.     return 0;  
    87. }  
  • 相关阅读:
    Make something people want
    POJ 3662 Telephone Lines (二分+Dijkstra)
    POJ 2010 Moo University
    HDU 6627 2017ACM/ICPC亚洲区域赛沈阳站 Rabbits
    HDU 6625 2017ACM/ICPC亚洲区域赛沈阳站 Little Boxes(高精度整数加法)
    Codeforces 877D Olya and Energy Drinks(BFS+剪枝)
    CodeForces 876B Divisiblity of Differences (数学水题)
    POJ 2139 Six Degrees of Cowvin Bacon (Floyd最短路)
    UESTC 1057 秋实大哥与花 (线段树)
    Linux安装Oh-my-zsh
  • 原文地址:https://www.cnblogs.com/as5346/p/4475353.html
Copyright © 2020-2023  润新知