Foundation3NSDictionary初始化方式

Dictionary初始化方式


NSDictionary是不可变的

// 最常用的初始化方式
    dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"v1", @"k1",
            @"v2", @"k2",
            @"v3", @"k3", nil];
    
	// 把键和值分别生成数组,放入Dictionary中
    NSArray *objects = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
    NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
    dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    NSLog(@"%@", dict);

Dictionary的内存管理方式
类似与Array
一个对象称为字典的key或者value时,会做一次retain操作,也就是计数器会+1
当字典被销毁时,里面的所有key和value都会做一次release操作,也就是计数器会-1




Dictionary的基本用法

 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
            @"v1", @"k1",
            @"v2", @"k2",
            @"v3", @"k3", nil];
    
    // count是计算有多少个键值对(key-value)
    NSLog(@"count=%zi", dict.count);
    
    // 根据键,提取对应的值(由于NSDictionary是不可变的,所以只能取值,而不能修改值)
    id obj = [dict objectForKey:@"k2"];
    NSLog(@"obj=%@", obj);
    
    // 将字典写入文件中(显示结果也Array一样是xml格式)
    NSString *path = @"/Users/apple/Desktop/dict.xml";
    [dict writeToFile:path atomically:YES];
    
    // 从文件中读取内容(读取的格式也必须是xml格式,格式参考写入文件时的格式)
    dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"dict=%@", dict);
}

    // 返回所有的key
    NSArray *keys = [dict allKeys];
    //NSLog(@"keys=%@", keys);
    
    // 返回所有的vlaues
    NSArray *objects = [dict allValues];
    //NSLog(@"objects=%@", objects);
    
    // 根据多个key取出对应的多个value
    // 当key找不到对应的value时,用marker参数(次代码中为:@"not-found")值代替
    NSArray *objects1 = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1", @"k2", @"k4", nil] notFoundMarker:@"not-found"];
    NSLog(@"objects1=%@", objects1);

遍历字典
有三种方式:


1.for特殊循环(for in方式)

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"v1", @"k1",
                          @"v2", @"k2",
                          @"v3", @"k3", nil];
    // 遍历字典的所有key
    for (id key in dict) {
        id value = [dict objectForKey:key];
        NSLog(@"%@=%@", key, value);
    }

2.key迭代器方式

NSEnumerator *enumer = [dict keyEnumerator];
    id key = nil;
    while ( key = [enumer nextObject]) {
        id value = [dict objectForKey:key];
        NSLog(@"%@=%@", key, value);
    }
    
    // 对象迭代器
    // [dict objectEnumerator];

3.block方式

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"v1", @"k1",
                          @"v2", @"k2",
                          @"v3", @"k3", nil];
// 参数:key 键,obj 值 stop 为YES时结束循环
    [dict enumerateKeysAndObjectsUsingBlock:
     ^(id key, id obj, BOOL *stop) {
        NSLog(@"%@=%@", key, obj);
    }];

NSMutableDictionary可变字典的使用

  // 创建一个空的字典,(因为可变,所以不一定需要在创建的时候赋值,可以创建之后添加。)
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    Student *stu1 = [Student studentWithName:@"stu1"];
    Student *stu2= [Student studentWithName:@"stu2"];

// 添加元素
    // stu1的计数器会+1
    [dict setObject:stu1 forKey:@"k1"];
    NSLog(@"stu1:%zi", [stu1 retainCount]);
    
    // 添加其他字典other到当前字典dict中
    NSDictionary *other = [NSDictionary dictionaryWithObject:@"v2" forKey:@"k2"];
    [dict addEntriesFromDictionary:other];
    
    // 删除所有的键值对
    // [dict removeAllObjects];
    
    // 删除k1对应的元素stu1,stu1会做一次release操作
    [dict removeObjectForKey:@"k1"];
    NSLog(@"stu1:%zi", [stu1 retainCount]);
    
    // 删除多个key对应的value
    // [dict removeObjectsForKeys:[NSArray arrayWithObject:@"k1"]];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dwt1220

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值