合并具有多个值的数组以形成一个数组/字典

时间:2012-12-18 11:20:32

标签: objective-c nsarray

我有一个包含服务器数据的数组。我无法改变服务器的响应。

{
    maximum = 30;
    minimum = 1;
    name = "Layer 1";
},
    {
    maximum = 60;
    minimum = 45;
    name = "Layer 1";
},
    {
    maximum = 60;
    minimum = 45;
    name = "Layer 2";
}

我在这个数组中有3个对象。其中2个具有相同的名称,但具有不同的最小值和最大值。我想创建一个不会有重复的数组,所以例如我的数组中有2个对象,"第1层","第2层"。第1层将具有2个最小值和最大值。

它应该如何:

{

    name = "Layer 1"; value = [{maximum = 30;
    minimum = 1},{ maximum = 60;
    minimum = 45}];
},
    {
    maximum = 60;
    minimum = 45;
    name = "Layer 2";
}

我试过检查"名称"在索引" i"等于"名称"索引" i + 1"但是它很糟糕,它说超越界限(2):

rows = [mivne_shichva count];

         NSMutableArray *layers = [[NSMutableArray alloc]init];
        NSMutableDictionary *layer = [[NSMutableDictionary alloc]init];

        for (int i = 0; i<rows; i++) {
            if ([[[mivne_shichva objectAtIndex:i]valueForKey:@"name"] isEqualToString:[[mivne_shichva objectAtIndex:i+1]valueForKey:@"name"]]) {
                NSLog(@"name equals to name in i+1");
            }
            [layer setValue:[[mivne_shichva objectAtIndex:i]valueForKey:@"name"] forKey:@"name"];
            [layer setValue:[[mivne_shichva objectAtIndex:i]valueForKey:@"minimum"] forKey:@"minimum"];
            [layer setValue:[[mivne_shichva objectAtIndex:i]valueForKey:@"maximum"] forKey:@"maximum"];

            [layers addObject:layer];

            NSLog(@"layers :::: %@",layers);            
        }

1 个答案:

答案 0 :(得分:1)

检查一下, 逻辑是

最终结果将在'字典'中。 'dictionary'键包含名称'Layer 1' 'dictionary'值包含'array'。 'array'包含'MinMax'的对象。

MinMax类:

@interface MinMax : NSObject

@property(strong)NSString *minimum;
@property(strong)NSString *maximum;

@end

MyClass类:

@interface MyClass : NSObject

@property(strong) NSString *name;
@property(strong) NSString *minimum;
@property(strong) NSString *maximum;

@end

演示实现:

- (id)init
{
    self = [super init];
    if (self) {
        _finalDict=[NSMutableDictionary new];

        MyClass *obj1=[MyClass new];
        obj1.name=@"Layer 1";
        obj1.minimum=@"1";
        obj1.maximum=@"30";

        MyClass *obj2=[MyClass new];
        obj2.name=@"Layer 1";
        obj2.minimum=@"45";
        obj2.maximum=@"60";

        MyClass *obj3=[MyClass new];
        obj3.name=@"Layer 2";
        obj3.minimum=@"45";
        obj3.maximum=@"60";

        _fromServerArrays=[[NSMutableArray alloc]initWithObjects:obj1, obj2, obj3, nil];

    }
    return self;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

    for (MyClass *obj in _fromServerArrays) {
        NSLog(@"From server  : %@, %@, %@", obj.name, obj.minimum, obj.maximum);
    }


    for (MyClass *myClassObj in _fromServerArrays) {
        MinMax *mmObj=[MinMax new];
        mmObj.minimum=myClassObj.minimum;
        mmObj.maximum=myClassObj.maximum;

        if ([_finalDict objectForKey:myClassObj.name]) {
            NSMutableArray *getArray=[_finalDict objectForKey:myClassObj.name];
            [getArray addObject:mmObj];
        }
        else{
            NSMutableArray *array=[NSMutableArray new];
            [array addObject:mmObj];
            [_finalDict setObject:array forKey:myClassObj.name];
        }
    }


    for (NSDictionary *dict in _finalDict) {
        NSLog(@"%@",dict);
        for (MinMax *mmObj in [_finalDict objectForKey:dict]) {
            NSLog(@"%@, %@", mmObj.minimum, mmObj.maximum);
        }
    }
}

输出:

From server  : Layer 1, 1, 30
From server  : Layer 1, 45, 60
From server  : Layer 2, 45, 60
Layer 1
1, 30
45, 60
Layer 2
45, 60