0

I have an array with data from server. I can't change the server's response.

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

I have 3 objects in this array. 2 of them are with the same name but with different minimum and maximum values. I want to create an array which won't have duplications, so for example I'll have 2 object in my array, "Layer 1", "Layer 2". Layer 1 will have the 2 minimum values, and maximum values.

How it should look like:

{

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

I have tried to check if the "name" of the object at index "i" is equals to the "name" of the object at index "i+1" but it crahses, it says "beyond bounds (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);            
        }
8
  • I'm sure you tried something, but it did not work. Would you mind sharing your latest attempt that was closest to your goal? We'll help you make it work. Commented Dec 18, 2012 at 11:23
  • Yeah, I have tried to check if the object at index "i" is equals to object at index i+1 but it crashes... says it beyond bounds (2)... I have added my code to my question Commented Dec 18, 2012 at 11:26
  • That's because your loop needs to go up to rows - 1, not up to rows, when you plan to use i + 1 as an index expression. Commented Dec 18, 2012 at 11:34
  • is this structure acceptable to make it generic ? { name = "Layer 1"; value = [ {maximum = 30, minimum = 1}, {maximum = 60, minimum = 45} ]; }, { name = "Layer 1"; value = [ {maximum = 60, minimum = 45} ]; } Commented Dec 18, 2012 at 11:34
  • can you give an example please? Commented Dec 18, 2012 at 11:35

1 Answer 1

1

Check this one, The Logic is as

The final result will be in a 'dictionary'. The 'dictionary' key contains the name as 'Layer 1' The 'dictionary' value contains an 'array'. The 'array' contains objects of 'MinMax'.

The MinMax class:

@interface MinMax : NSObject

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

@end

The MyClass class:

@interface MyClass : NSObject

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

@end

And the demo implementation:

- (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);
        }
    }
}

Output:

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
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this kind of algo already... i don't know how to implement it as a working code... :-/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.