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. }  
  • 相关阅读:
    Spring AOP前置通知实例说明AOP相关概念
    什么是面向切面编程AOP
    关于IOC容器的一些个人理解
    在.Net Core WebAPI下给Swagger增加导出离线文档功能
    .Net Core ORM选择之路,哪个才适合你
    真香.小程序云开发(时光邮局小程序)
    Cordova的安装与配置
    JS三座大山再学习(三、异步和单线程)
    JS三座大山再学习(二、作用域和闭包)
    JS三座大山再学习(一、原型和原型链)
  • 原文地址:https://www.cnblogs.com/as5346/p/4475353.html
Copyright © 2011-2022 走看看