NSMutableDictionary 的变态特性 覆盖性! 合并两个 NSMutableDictionary

NSMutableDictionary添加对象时,key已经存在。怎么继续添加对象,将会覆盖原来的值。

原问题:

NSMutableDictionary添加对象时,关键字已经存在。怎么继续添加对象,不覆盖原来的值,也不改变关键字。比如 NSMutableDictionary *nameSection = [[NSMutableArray alloc] init]; [nameSection setObject:@"Boy" forKey:@"B"]; 现在要添加一个[nameSection setObject:@"Bill" forKey:@"B"];会覆盖前面的“Boy”,要怎么样才能不覆盖? 我知道有个类似的方法: NSMutableDictionary *names = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSArray arrayWithArray:@“Boy”, @"Bill", nil], @"B", nil]; 但是如果names已经初始化完了,以后某个时刻需要添加对象@“Bug”,关键字还是@“B”,该怎么添加?

解决方案:在字典里面 在添加可变数组  一个B里面 有N个数据

例子代码:

//添加数据到列表
-(void)appendTableWith:(NSMutableDictionary *)data
{
    
    NSArray *arrId = [dic_tabelViewDataSource objectForKey:@"id"];
    NSArray *arrName = [dic_tabelViewDataSource objectForKey:@"name"];
    NSArray *arrDesc = [dic_tabelViewDataSource objectForKey:@"desc"];
    NSArray *arrImages = [dic_tabelViewDataSource objectForKey:@"images"];
    NSArray *arrStar = [dic_tabelViewDataSource objectForKey:@"star"];
    NSArray *arrRecommendation = [dic_tabelViewDataSource objectForKey:@"recommendation"];
    NSArray *arrPrice = [dic_tabelViewDataSource objectForKey:@"price"];
    
    //将data中的所有数据遍历出来并传递给dic_tabelViewDataSource
    NSMutableArray *nsma_id = [[NSMutableArray alloc]initWithArray:arrId];
    NSMutableArray *nsma_name = [[NSMutableArray alloc]initWithArray:arrName];
    NSMutableArray *nsma_desc = [[NSMutableArray alloc]initWithArray:arrDesc];
    NSMutableArray *nsma_images = [[NSMutableArray alloc]initWithArray:arrImages];
    NSMutableArray *nsma_star = [[NSMutableArray alloc]initWithArray:arrStar];
    NSMutableArray *nsma_recommendation = [[NSMutableArray alloc]initWithArray:arrRecommendation];
    NSMutableArray *nsma_price = [[NSMutableArray alloc]initWithArray:arrPrice];
    
    NSEnumerator * enumerator = [data keyEnumerator];//把keyEnumerator替换为objectEnumerator即可得到value值(1)
    
    NSArray *more_id = [[NSArray alloc]init];
    NSArray *more_name = [[NSArray alloc]init];
    NSArray *more_desc = [[NSArray alloc]init];
    NSArray *more_images = [[NSArray alloc]init];
    NSArray *more_star = [[NSArray alloc]init];
    NSArray *more_recommendation = [[NSArray alloc]init];
    NSArray *more_price = [[NSArray alloc]init];
    
    //定义一个不确定类型的对象
    id object;
    //遍历输出
    while(object = [enumerator nextObject])
    {
//        NSLog(@"键值为:%@",object);
        //添加id
        if ([object isEqualToString:@"id"]) {
            more_id = [data objectForKey:@"id"];
            for (int i=0; i<[more_id count]; i++) {
                [nsma_id addObject:[more_id objectAtIndex:i]];
            }
            [dic_tabelViewDataSource setObject:nsma_id forKey:@"id"];
        }
        if ([object isEqualToString:@"name"]) {
            more_name = [data objectForKey:@"name"];
            for (int i=0; i<[more_name count]; i++) {
                [nsma_name addObject:[more_name objectAtIndex:i]];
            }
            [dic_tabelViewDataSource setObject:nsma_name forKey:@"name"];
        }
        if ([object isEqualToString:@"desc"]) {
            more_desc = [data objectForKey:@"desc"];
            for (int i=0; i<[more_desc count]; i++) {
                [nsma_desc addObject:[more_desc objectAtIndex:i]];
            }
            [dic_tabelViewDataSource setObject:nsma_desc forKey:@"desc"];
        }
        if ([object isEqualToString:@"images"]) {
            more_images = [data objectForKey:@"images"];
            for (int i=0; i<[more_images count]; i++) {
                [nsma_images addObject:[more_images objectAtIndex:i]];
            }
            [dic_tabelViewDataSource setObject:nsma_images forKey:@"images"];
        }
        if ([object isEqualToString:@"star"]) {
            more_star = [data objectForKey:@"star"];
            for (int i=0; i<[more_star count]; i++) {
                [nsma_star addObject:[more_star objectAtIndex:i]];
            }
            [dic_tabelViewDataSource setObject:nsma_star forKey:@"star"];
        }
        if ([object isEqualToString:@"recommendation"]) {
            more_recommendation = [data objectForKey:@"recommendation"];
            for (int i=0; i<[more_recommendation count]; i++) {
                [nsma_recommendation addObject:[more_recommendation objectAtIndex:i]];
            }
            [dic_tabelViewDataSource setObject:nsma_recommendation forKey:@"recommendation"];
        }
        if ([object isEqualToString:@"price"]) {
            more_price = [data objectForKey:@"price"];
            for (int i=0; i<[more_price count]; i++) {
                [nsma_price addObject:[more_price objectAtIndex:i]];
            }
            [dic_tabelViewDataSource setObject:nsma_id forKey:@"price"];
        }
    }
    [commonTableView reloadData];
}

 

总结学习:

1、初始化 NSMutableArray 时可以用一个NSArray 

NSMutableArray *nsma_id = [[NSMutableArray alloc]initWithArray:arrId];

当为
NSMutableArray 增加新的值,要用
[nsma_price addObject:[more_price objectAtIndex:i]];

 

posted @ 2013-05-20 22:34  ygm900  阅读(2913)  评论(0编辑  收藏  举报