0

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

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

我在这个数组中有 3 个对象。其中 2 个名称相同,但最小值和最大值不同。我想创建一个没有重复的数组,例如,我的数组中有 2 个对象,“Layer 1”,“Layer 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);            
        }
4

1 回答 1

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
于 2012-12-18T11:55:27.870 回答