• 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. }  
  • 相关阅读:
    日期型数据知识
    如何让VS检查函数和类Comment的添加情况
    HTTP request is unauthorized with client authentication scheme 'Anonymous'.
    将SerializableAttribute序列化为xml
    使用EnterpriseLibrary Validation Block对WCF做验证
    表达式树中递归方法
    使用SignalR在Asp.net中实现进度条
    SQLServer中列出数据库的所有表的创建时间
    用Knockoutjs与Asp.net MVC实现级联下拉列表
    使用UnityAutoMoq简化单元测试
  • 原文地址:https://www.cnblogs.com/as5346/p/4475353.html
Copyright © 2020-2023  润新知