我从服务器获取这种数据数组(它动态变化):
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";
}
);
在这种情况下,我在 1 个数组中有 5 个对象。每个对象都有名称、最小值和最大值。
代码应该有一个从最小值到最大值统计的循环。例如,如果最小值为 10,最大值为 13,则值应为:10,11,12,13。
我想将具有相同名称的对象放在一起,成为 1 个对象。正如我所说,这些数据是动态变化的。它可以有任何随机数据,但如果有相同的对象,它们将一个接一个地放置,就像这里的“第 1 层”对象和“第 3 层”对象一样。
我想要的最终结果是这样的:
{
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
);
}
我试图运行的代码是:
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");
}
}
但我不明白如何使它工作......
提前感谢任何代码和帮助!