zoukankan      html  css  js  c++  java
  • [OC Foundation框架

    通过唯一的key找到相应的value,类似于Map

    NSDictionary是不可变的
     
    1.创建
    复制代码
     1 void dicCreate()
     2 {
     3     //Immutable
     4 //    NSDictionary *dic = [NSDictionary dictionary];
     5    
     6     NSDictionary *dic = [NSDictionary dictionaryWithObject:@"Simon" forKey:@"name"];
     7    
     8     dic = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];
     9    
    10     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
    11     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
    12     dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
    13    
    14     NSLog(@"%@", dic);
    15    
    16 }
    复制代码
     
    集中批量创建字典
    1         NSDictionary *d11_1 = @{@"姓名":@"张三", @"年龄":@"21", @"性别":@"男"};
     
    若存在同名的元素,采用最先定义的,多余的元素不会被计数
    不允许nil作为键值
    NSMutableDictioinary不能使用此方法,因为返回的是一个NSDictionary
     
     
    2.基本操作
    (1)键值对数量
    (2)比较
    (3)取值
    (4)IO操作
    复制代码
     1 void dicUse()
     2 {
     3     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
     4     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
     5     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
     6    
     7     NSLog(@"%@", [dic objectForKey:@"k1"]);
     8 }
     9  
    10 void dicUse2()
    11 {
    12     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v1", @"v2", nil];
    13     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
    14     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
    15    
    16     NSArray *keys2 = [dic allKeys];
    17     NSLog(@"%@", keys2);
    18    
    19     NSArray *keys3 = [dic allKeysForObject:@"v1"];
    20     NSLog(@"%@", keys3);
    21    
    22    
    23     NSArray *objs2 = [dic objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2",@"k4",nil] notFoundMarker:@"not found"];
    24     NSLog(@"%@", objs2);
    25 }
    复制代码
     
    3.遍历
    复制代码
     1 void dicLoop()
     2 {
     3     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
     4     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
     5     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
     6    
     7     //Loop all keys in dictionary
     8     for (id key in dic)
     9     {
    10         id value = [dic objectForKey:key];
    11         NSLog(@"%@ = %@", key, value);
    12     }
    13 }
    复制代码
     
    4.迭代器
    复制代码
     1 void dicEnumerator()
     2 {
     3     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
     4     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
     5     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
     6    
     7     //key enumerator
     8     NSEnumerator *enumerator = [dic keyEnumerator];
     9     id key =nil;
    10     while (key = [enumerator nextObject])
    11     {
    12         id value = [dic objectForKey:key];
    13         NSLog(@"%@ = %@", key, value);
    14     }
    15 }
    复制代码
     
    5.block迭代
    复制代码
     1 void dicBlockLoop()
     2 {
     3     NSArray *objs = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
     4     NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
     5     NSDictionary *dic = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
     6    
     7     [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
     8         NSLog(@"%@ - %@", key, obj);
     9     }];
    10 }
    复制代码
     
    6.内存管理
    复制代码
     1 void memoryManage()
     2 {
     3     Student *stu1 = [Student studentWithName:@"Simon"];
     4     Student *stu2 = [Student studentWithName:@"Joke"];
     5     Student *stu3 = [Student studentWithName:@"Man"];
     6     NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:stu1,@"stu1",
     7                                                                     stu2, @"stu2",
     8                                                                     stu3, @"stu3", nil];
     9    
    10    
    11      //When dictionary is destroyed, the keys & objects will be released one time
    12     NSLog(@"stu1 before dic release:%zi", stu1.retainCount);
    13 }
    复制代码
     
    7.取出键、值
    (1)
    1         NSArray *da = [d allKeys];
    (2)
    1         NSLog(@"%@", d[@"6"]);
     
    8.NSDictionary是无序的,取出来的值不会按照存入的顺序排列
    复制代码
    1         NSMutableDictionary *d = [NSMutableDictionary dictionary];
    2         for (int i=0; i<100; i++)
    3         {
    4             [d setObject:@"abc" forKey:[NSString stringWithFormat:@"%d", i]];
    5         }
    6        
    7         NSLog(@"%@", d);
    复制代码
     
    out:
    2014-11-19 02:36:25.850 03-NSArray[5171:303] {
        0 = abc;
        1 = abc;
        10 = abc;
        11 = abc;
    ...
     
    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    [Linux/wine.笔记]关于WINE(Linux上运行Windows程序的兼容层)
    [docker.笔记]常用命令
    [技巧.DotNet]超级好用的动态对象ExpandoObject
    .net core 的窗体设计器进展(.NET Core Windows Forms designer),5月中旬或将发布成熟版!
    [问题记录.Oracle/odp.net]托管ODP中,连接池的连接验证参数(validate connection=true)无效?
    [JWT]Json Web Token 备忘
    [MQ]RabbitMQ的概要介绍及消息路由规则
    常见排序算法
    C语言数值存储溢出探讨
    从计算理解数组
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/4513069.html
Copyright © 2011-2022 走看看