0

好的,我基本上需要找到一种方法来更好地管理 100 个 NSMutableArrays 的数据。

就像,(我实际上没有这样做):

NSMutableArray *array1;
NSMutableArray *array2;
NSMutableArray *array3;
//etc..

我需要具有多个方面的东西,例如:

NSMutableCombinationArray *theMassiveArray;

[theMassiveArray addObject:theObject forArray:0 atIndex:0];

最后我想做这样的事情:

//in applicationdidfinishlaunching
loopCount = 0;


- (void) frameTimer:(NSTimer*)timer { //called every second

    startingValue = 0;

    for (int i = 1; i <= 100; i++)
    {
        NSString *dmxValues = [NSString stringWithFormat: @"%@,%@,%@,%@,%@", [DMXChannelArray objectAtIndex:startingValue], [DMXChannelArray objectAtIndex:startingValue+1], [DMXChannelArray objectAtIndex:startingValue+2], [DMXChannelArray objectAtIndex:startingValue+3], [DMXChannelArray objectAtIndex:startingValue+4]];

        [theMassiveArray addObject:dmxValues forArray:i-1 atIndex:loopCount];

        startingValue = startingValue + 5;
    }

    loopCount = loopCount + 1;

}

//NOTE DMXChannelArray is updated in another class with current DMX values from a controller.

所以DMX有512个通道,每个阵列需要容纳5个通道。这是一个记录应用程序,基本上以每秒一次的分辨率存储 500 个 DMX 通道值。

我怎样才能做到这一点?最好的方法是什么?我显然想避免编写 100 个 NSMutableArrays 并手动完成这一切。

作为参考,当录制结束时,用户选择他想要导出多少组(5 个通道),然后根据该数量,另一个 for 循环将通过 NSMutableCombinationArray 并导出最终数组。

4

3 回答 3

2

编写一个包装类:

@interface MultiDimensionalMutableArray : NSObject

- (void)insertObject:(id)obj atIndex:(NSUInteger)idx intoArrayAtIndex:(NSUInteger)arrIdx;
- (id)objectAtIndex:(NSUInteger)idx inArrayAtIndex:(NSUInteger)arrIdx;

@end

@implementation MultiDimensionalMutableArray
{
    NSMutableArray * arrays;
}

- (id)init
{
    // init boilerplate

    arrays = [NSMutableArray arrayWithCapacity:100];
    for( int i = 0; i < 100; i++ ){
        [arrays addObject:[NSMutableArray array]];
    }

    return self;
}

- (void)insertObject:(id)obj atIndex:(NSUInteger)idx intoArrayAtIndex:(NSUInteger)arrIdx
{
    //TODO: Include appropriate bounds checking
    NSMutableArray * innerArray = [arrays objectAtIndex:arrIdx];
    [innerArray insertObject:obj atIndex:idx];
}

- (id)objectAtIndex:(NSUInteger)idx inArrayAtIndex:(NSUInteger)arrIdx
{
    return [[arrays objectAtIndex:arrIdx] objectAtIndex:idx];
}

@end
于 2013-02-06T21:43:32.153 回答
1

这是你应该做的

NSMutableArray *massiveArray = [[NSMutableArray alloc] init];

for(int i=0; i<500; i+=5) {
    NSDictionary *dataDict = [[NSDictionary alloc] initWithObjectsAndKeys:[DMXChannelArray objectAtIndex:i], @"Key1", [DMXChannelArray objectAtIndex:i+1], @"Key2", [DMXChannelArray objectAtIndex:i+2], @"Key3", [DMXChannelArray objectAtIndex:i+3], @"Key4", [DMXChannelArray objectAtIndex:i+4], @"Key5", nil];
    [massiveArray addObject:dataDict];        
}

所以最后 massArray 包含 100 个字典,例如,要访问第 45 个字典的第二个值,您可以这样做:

NSString *value = [[massiveArray objectAtIndex:44] objectForKey:@"Key2"];

希望这可以帮助

于 2013-02-06T21:43:23.880 回答
0

NSMutableDictionary *dict = [NSMutableDictionary alloc] initWithCapacity:100];
for(int i=1; i<=100; i++){
  NSString *dmxValues = [NSString stringWithFormat: @"%@,%@,%@,%@,%@", [DMXChannelArray objectAtIndex:startingValue], [DMXChannelArray objectAtIndex:startingValue+1], [DMXChannelArray objectAtIndex:startingValue+2], [DMXChannelArray objectAtIndex:startingValue+3], [DMXChannelArray objectAtIndex:startingValue+4]];
  [dict setObject:[NSArray arrayWithObject:dmxValues] forKey:[NSNumber numberWithInt:(i-1)];
}

检索您的值

[dic objectForkey:[NSNumber numberWithInt:array_index][array_element_index];
于 2013-02-06T21:45:08.650 回答