合并与对Dictionary中包含的Array中的对象进行排序

时间:2012-12-20 12:12:23

标签: objective-c ios4 nsmutablearray

我从服务器获取此类数据(动态更改):

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层”对象和“第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");
                }



        }

但我不明白如何使其有效......

感谢您提前获取任何代码并提供帮助!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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] ;
}