Reputation: 441
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);
}
Upvotes: 0
Views: 218
Reputation: 46543
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
Upvotes: 1