zoukankan      html  css  js  c++  java
  • [Objective-C] 009_Foundation框架之NSDictionary与NSMutableDictionary

      在Cocoa Foundation中NSDictionary和NSMutableDictionary 用于对象有序集合,NSDictionary和NSMutableDictionary 与 NSArray和NSMutableArray 有些不一样,前者可以保存多个不同类型的数据,而后者只能保存同一类型的数据。

    1.NSDictionary 创建

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];

    2.NSDictionary 遍历

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];
    
    //得到词典的数量
    int objectCount = (int)[dictionary count];
    NSLog(@"词典的KEY数量为: %d",objectCount);
    
    //获取词典中所有KEY值
    NSEnumerator * enumeratorKey = [dictionary keyEnumerator];
    //快速枚举遍历所有KEY的值
    for (NSObject *object in enumeratorKey) {
        NSLog(@"KEY的值---》: %@",object);
    }
    
    //获取词典中所有Value值
    NSEnumerator * enumeratorValue = [dictionary objectEnumerator];
    //快速枚举遍历所有Value的值
    for (NSObject *object in enumeratorValue) {
        NSLog(@"Value的值---》: %@",object);
    }

    3.NSDictionary 通过KEY找到的value

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SuperDo",@"teamName",@"SuperDo.Horse",@"teamMember", nil];
    NSObject *object = [dictionary objectForKey:@"teamName"];
    if (object != nil) {
        NSLog(@"通过KEY找到的value是: %@",object);
    }

    4.NSMutableDictionary 创建

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:10];
    

    5.NSMutableDictionary 元素操作

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:10];
        
    //向词典中动态添加数据
    [dictionary setObject:@"SuperDo" forKey:@"teamName"];
    [dictionary setObject:@"SuperDo.Horse" forKey:@"teamMember"];
    
    //通过KEY找到value
    NSObject *object = [dictionary objectForKey:@"teamName"];
    
    //获取所有的key
    NSArray *allKeys = [dictionary allKeys];
    
    //获取所有的value
    NSArray *allValues = [dictionary allValues];
    

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4594337.html

     

  • 相关阅读:
    codevs 3657 括号序列
    洛谷P1962 斐波那契数列
    Black Rock shooter
    codevs 2596 售货员的难题
    51Nod-1154 回文串划分
    UVA
    POJ3321[苹果树] 树状数组/线段树 + dfs序
    Hdu 4578 Transformation (线段树 分类分析)
    786B
    438D
  • 原文地址:https://www.cnblogs.com/superdo/p/4594337.html
Copyright © 2011-2022 走看看