zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    摘记
    【题解】网格 & Single Cut of Failure(trick:答案上界)
    题解 CF1404C 【Fixed Point Removal】
    Linux IO模型知识梳理
    Java IO模型知识梳理
    如何回答什么是线程安全?
    MySQL主从复制与备份
    MySQL的高性能索引策略
    CAS(乐观锁)的原理解析
    Java虚拟机的类加载机制
  • 原文地址:https://www.cnblogs.com/as5346/p/4475353.html
Copyright © 2011-2022 走看看