NSDictionary 字典

#pragma mark ------------- NSDictionary ---------

#pragma mark 创建字典

    NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"mm" forKey:@"girlFriend"];//??存储中文字符
    NSLog(@"dic1 : %@",dic1);

#pragma mark 创建多个字典
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"wsl",@"name",
                          @"m",@"gender",
                          @"22",@"age",
                          nil];

    NSLog(@"dic2 = %@",dic2);

#pragma mark 根据现有字典创建字典
    NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];
    NSLog(@"dic3 = %@",dic3);

#pragma mark 根据 key值 获取 value
    NSLog(@"name value: %@",[dic3 objectForKey:@"name"]);

#pragma mark 获取字典数量
    NSLog(@"dic3 count: %lu",dic3.count);

#pragma mark 所有的键集合
    NSArray *keys = [dic3 allKeys];
    NSLog(@"allKeys : %@",keys);
#pragma mark 所有的值集合
    NSArray *values = [dic3 allValues];
    NSLog(@"allValues : %@",values);


#pragma mark  ----------NSMutableDictionary------------

    NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:dic3];
#pragma mark 添加现有字典数据
    [mutableDic addEntriesFromDictionary:dic1];
    NSLog(@"mutableDic : %@",mutableDic);
#pragma mark 添加新的键值对象
    [mutableDic setObject:@"18369906788" forKey:@"Phone"];
    NSLog(@"mutableDic : %@",mutableDic);

#pragma mark 以新的字典数据覆盖旧的字典数据
//    [mutableDic setDictionary:dic2];
//    NSLog(@"mutableDic : %@", mutableDic);

#pragma mark 根据key值删除value
    [mutableDic removeObjectForKey:@"age"];
    NSLog(@"mutableDic : %@",mutableDic);
#pragma mark 根据key数组删除元素
    NSArray *array = @[@"name",@"Phone"];
    [mutableDic removeObjectsForKeys:array];
    NSLog(@"removeObjectForKeys : %@",mutableDic);
#pragma mark 删除所有元素
    [mutableDic removeAllObjects];
    NSLog(@"remove all : %@",mutableDic);
#pragma mark 快速遍历

    for (id key in mutableDic) {
        NSLog(@"key : %@ value : %@",key,[mutableDic objectForKey:key]);
    }

#pragma mark 枚举遍历

    NSEnumerator *enumerator = [mutableDic keyEnumerator];
    id key = [enumerator nextObject];
    while (key) {
        NSLog(@"enumerator : %@",[mutableDic objectForKey:key]);
        key = [enumerator nextObject];
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值