I’m getting this kind of array of data from a server (it dynamically changes):
layers = (
{
maximum = 4;
minimum = 3;
name = "layer 1";
},
{
maximum = 19;
minimum = 8;
name = "layer 1";
},
{
maximum = 1;
minimum = 1;
name = "layer 38";
},
{
maximum = 4;
minimum = 1;
name = "layer 3";
},
{
maximum = 24;
minimum = 15;
name = "layer 3";
}
);
in this case, I have 5 objects in 1 array. every object has name, minimum value and maximum value.
The code should have a loop which stats from the minimum value to the maximum value. for example, if the minimum is 10 and maximum is 13, the values should be: 10,11,12,13.
I want to put together the objects with the same name, into 1 object. As I said, this data is dynamically changes. it can has any random data, but if there are equal objects, they will be placed one after each other, like here with “layer 1” objects and “layer 3” objects.
The final result I want is this:
{
name = "layer 1";
values = (
3,
4,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19
);
},
{
name = "layer 38";
values = (
1,
1
);
},
{
name = "layer 3";
values = (
1,
2,
3,
4,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24
);
}
The code I tried to run is:
layers = [[NSMutableArray alloc]init];
NSMutableArray *layer = [[NSMutableArray alloc]init];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[layers addObject:[mivne_shichva objectAtIndex:0]];
for (int i=0; i<mivne_shichva.count; i++) {
// NSLog(@"i=%d",i)
if ([[[mivne_shichva objectAtIndex:i]valueForKey:@"name"]isEqualToString:[[layers objectAtIndex:i] valueForKey:@"name"]]) {
NSLog(@"EQUALS");
layer = [[NSMutableArray alloc]init];
int minimum = [[[mivne_shichva objectAtIndex:i]valueForKey:@"minimum"]intValue];
int maximum = [[[mivne_shichva objectAtIndex:i]valueForKey:@"maximum"]intValue];
for (int x = minimum; x<=maximum; x++) {
[layer addObject:[NSString stringWithFormat:@"%d",x]];
NSLog(@"%@",layer);
// dic = [[NSMutableDictionary alloc]init];
[dic setValue:[[mivne_shichva objectAtIndex:i]valueForKey:@"name"] forKey:@"name"];
[dic setValue:layer forKey:@"values"];
[layers addObject:dic];
}
}else {
layer = [[NSMutableArray alloc]init];
int minimum = [[[mivne_shichva objectAtIndex:i]valueForKey:@"minimum"]intValue];
int maximum = [[[mivne_shichva objectAtIndex:i]valueForKey:@"maximum"]intValue];
for (int x = minimum; x<=maximum; x++) {
[layer addObject:[NSString stringWithFormat:@"%d",x]];
NSLog(@"%@",layer);
}
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setValue:[[mivne_shichva objectAtIndex:i]valueForKey:@"name"] forKey:@"name"];
[dic setValue:layer forKey:@"values"];
[layers addObject:dic];
NSLog(@"NOT EQUALS");
}
}
But I don’t understand how to make it works…
THANKS IN ADVANCE FOR ANY CODE AND HELP!
Your sortedArrayDict contains
keyas Layer#, andvalueas an array with sorted and merged value based on same layers.Layer Class :
Output: