3

我从服务器获取这种数据数组(它动态变化):

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



        }

但我不明白如何使它工作......

提前感谢任何代码和帮助!

4

2 回答 2

1

您的 sortedArrayDict 包含key为 Layer#,以及value基于相同层的排序和合并值的数组。

图层类:

@interface Layer : NSObject

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

@end

#import "AppDelegate.h"
#import "Layer.h"

@implementation AppDelegate

- (id)init
{
    self = [super init];
    if (self) {

        _finalDictionary=[NSMutableDictionary new];

        Layer *layer1=[Layer new];
        layer1.name=@"Layer 1";
        layer1.minimum=3;
        layer1.maximum=4;

        Layer *layer2=[Layer new];
        layer2.name=@"Layer 1";
        layer2.minimum=8;
        layer2.maximum=19;

        Layer *layer3=[Layer new];
        layer3.name=@"Layer 38";
        layer3.minimum=1;
        layer3.maximum=1;

        Layer *layer4=[Layer new];
        layer4.name=@"Layer 1";
        layer4.minimum=2;
        layer4.maximum=4;

        Layer *layer5=[Layer new];
        layer5.name=@"Layer 3";
        layer5.minimum=24;
        layer5.maximum=15;

        _initialArray=[[NSMutableArray alloc]initWithObjects:layer1,layer2,layer3,layer4,layer5, nil];

    }
    return self;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

    for (Layer *layer in _initialArray) {
        if ([_finalDictionary objectForKey:layer.name]) {
            //append array
            NSMutableArray *array=[_finalDictionary objectForKey:layer.name];
            [array addObject:[NSNumber numberWithInteger:[layer minimum]]];
            [array addObject:[NSNumber numberWithInteger:[layer maximum]] ];
        }
        else{
            //add new key
            NSMutableArray *array=[NSMutableArray new];
            [array addObject:[NSNumber numberWithInteger:[layer minimum]]];
            [array addObject:[NSNumber numberWithInteger:[layer maximum]] ];
            [_finalDictionary setObject:array forKey:layer.name];
        }
    }

    //sorting
    for (int i=0; i<_finalDictionary.count; i++){
        NSString *dict=[[_finalDictionary allKeys] objectAtIndex:i];
        NSArray *arrayToSort=[_finalDictionary objectForKey:dict];
        NSArray *sortedArray=[arrayToSort sortedArrayUsingSelector:@selector(compare:)];
        [_finalDictionary setObject:sortedArray forKey:dict];
    }

    //printing
    for (NSDictionary *dict in _finalDictionary) {
        NSLog(@"Label: %@",dict);
        for (Layer *layer in [_finalDictionary objectForKey:dict]) {
            NSLog(@"Value: %@",layer);
        }
    }
}

输出:

2012-12-20 18:45:32.344 MergeArray[1357:403] Label: Layer 1
2012-12-20 18:45:32.346 MergeArray[1357:403] Value: 2
2012-12-20 18:45:32.347 MergeArray[1357:403] Value: 3
2012-12-20 18:45:32.347 MergeArray[1357:403] Value: 4
2012-12-20 18:45:32.348 MergeArray[1357:403] Value: 4
2012-12-20 18:45:32.349 MergeArray[1357:403] Value: 8
2012-12-20 18:45:32.349 MergeArray[1357:403] Value: 19
2012-12-20 18:45:32.350 MergeArray[1357:403] Label: Layer 38
2012-12-20 18:45:32.350 MergeArray[1357:403] Value: 1
2012-12-20 18:45:32.351 MergeArray[1357:403] Value: 1
2012-12-20 18:45:32.352 MergeArray[1357:403] Label: Layer 3
2012-12-20 18:45:32.360 MergeArray[1357:403] Value: 15
2012-12-20 18:45:32.362 MergeArray[1357:403] Value: 24
于 2012-12-20T12:43:09.800 回答
0
//array 'layers' of sample data
NSArray* layers = @[ @[@19,@8,@"layer 1"] , @[@4,@3,@"layer 1"] , @[@1,@1,@"layer 38"] , @[@4,@1,@"layer 3"] , @[@24,@15,@"layer 3"] ] ;

NSMutableDictionary* results = [NSMutableDictionary new] ;
for ( NSArray* layer in layers ) {
    int layerMaximum = [layer[0] intValue] ;
    int layerMinimum = [layer[1] intValue] ;
    NSString* layerName = layer[2] ;

    NSMutableArray* layerValues = [results objectForKey:layerName] ;
    if ( ! layerValues ) {
        layerValues = [NSMutableArray new] ;
    }

    for ( int i = layerMinimum ; i <= layerMaximum ; i++ ) {
        NSNumber* value = [NSNumber numberWithInt:i] ;
        if ( ! [layerValues containsObject:value] ) {
            [layerValues addObject:value] ;
        }
    }
    if ( layerMinimum == layerMaximum ) {
        [layerValues addObject:[NSNumber numberWithInt:layerMaximum] ] ;
    }
    [results setObject:layerValues forKey:layerName] ;
}

for ( NSString* layerName in results.allKeys ) {
    NSMutableArray* values = [results objectForKey:layerName] ;
    NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:YES] ;
    [values sortUsingDescriptors: @[sortDescriptor] ] ;
    [results setObject:values forKey:layerName] ;
}
于 2012-12-20T14:01:47.543 回答