美文网首页
NSDictionary

NSDictionary

作者: 满大街都是大卡车 | 来源:发表于2016-09-29 11:00 被阅读0次

    在Foundation框架中,字典(NSDictionary、NSMutableDictionary)是由键-值对组成的数据集合,可以通过键(Key)查找值(Value),Key通常为字符串类型,也可以是其他类型,并且Key在字典中是唯一的。此外,字典对象的键和值不能为空(nil),如需要表示空值可以使用NSNull对象。
    备注:字典中的元素是无序的。

    创建和初始化:
    NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectAndKeys:Value, Key, nil];
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:Value, Key, nil]; // 类方法
    
    // 创建时初始化一个元素
    NSDictionary *dic1 = [NSDictionary dictionaryWithObject:Value, forKey: Key];
    
    // 初始化新字典,新字典包含一个字典
    NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:dic2];
    
    常用方法:
    // 取得字典中个数
    NSUinteger count = dic1.count;
    
    // 取得字典中所有的Key,等价于dic1.allKeys;
    NSArray *allKey = [dic1 allKeys];
    
    // 取得字典中所有的Value
    NSArray *allValue = [dic1 allValue];
    
    // 通过Key取得Value
    NSArray *array2 = [dic1 objectForKey:@"shen"]; 
    
    // 获取相同Key的所有Value
    [dic1 valueForKeyPath:Key];
    
    // 将字典中元素赋值到self的相应属性
    [self setValuesForKeysWithDictionary:dic1];
    
    // 将字典的Key转成枚举对象,用于遍历
    NSEnumerator *enumerator = [dic1 keyEnumerator];
    
    优化语句:
    // 创建
    NSDictionary *dic2 = @{Key:Value, Key:Value};
    
    // 取值
    NSArray *array3 = dic1[@"shen"];
    
    读写文件:
    // 文件路径
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Document/dic.plist"];
    
    // 写文件
    BOOL success = [array writeToFile:path atomically:YES];
    
    // 读文件
    NSDictionary *readDic = [[NSDictionary dictionaryWithContentsOfFile:path];
    

    NSMutableDictionary

    创建:
    NSMutableDictionary *muDic1 = [[NSMutableDictionary alloc] initWithCapacity:3];
    
    NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: Value, Key, nil];
    
    NSDictionary *dic3 = [NSDictionary dictionaryWithObject: Value forKey: Key];
    
    添加元素:
    // 添加一个元素
    [muDic1 setObject:array1 forKey:@"shen"];
    
    // 将字典dic1中所有元素添加到muDic1中
    [muDic1 addEntriesFromDictionary:dic1];
    
    移除元素:
    // 根据Key移除元素
    [muDic1 removeObjectForKey:@"shen"];
    
    // 移除所有元素
    [muDic1 removeAllObjects];
    
    // 根据多个Key移除元素
    [muDic1 removeObjectFormKeys:@[@"shen", @"shi"]];
    
    字典遍历
    // 快速遍历:
    for (NSString *key in muDic1) {
         NSArray *n = [muDic1 objectFoeKey:key];
         NSLog("@Key=%@, Value=%@", key, n);
    }
    
    // 普通遍历:
    NSArray *allKeys = [muDic1 allKeys];
    for (int i = 0; i < allValues.count; i ++) {
         NSString *key = allKeys[i];
         NSArray *n = muDic1[key];
    NSLog("@Key=%@, Value=%@", key, n);  
    }
     
    // 枚举遍历:
    NSEnumerator *enumerator = [dic keyEnumerator];
    id key = [enumerator nextObject];
    while (key) {
         id obj = [dic objectForKey:key];
         NSLog(@"%@", obj);
         key = [enumerator nextObject];
    }
    

    相关文章

      网友评论

          本文标题:NSDictionary

          本文链接:https://www.haomeiwen.com/subject/pabkyttx.html