我有一个包含来自服务器的数据的数组。我无法更改服务器的响应。
{
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);
}