首页 > 代码库 > NSMutableDictionary

NSMutableDictionary

#pragma mark 可变字典的使用。

void dictUse() {

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    Student *stu = [Student studentWithName:@"stu"];

    Student *stu1 = [Student studentWithName:@"stu1"];

    Student *stu2 = [Student studentWithName:@"stu2"];

    

    //给字典设置一个键值对。

    [dict setObject:stu forKey:@"k"];

    

    //添加其他字典到当前字典中。

    [dict addEntriesFromDictionary:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:stu1, stu2, nil] forKeys:[NSArray arrayWithObjects:@"k1", @"k2", nil]]];

    

    //删除所有键值对

    //[dict removeAllObjects];

 

    //删除k对应的元素stu,stu会做一次release操作。

    [dict removeObjectForKey:@"k"];

    NSLog(@"%@",dict);

    

    //删除数组中所有key值对应的元素。

    [dict removeObjectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2", nil]];

    NSLog(@"%@",dict);

    

}

NSMutableDictionary