好的,我基本上需要找到一种方法来更好地管理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.
因此,有512个DMX通道,每个阵列需要容纳5个通道。这是一个录制应用程序,基本上以每秒一次的分辨率存储500个DMX通道值。
我怎样才能做到这一点?最好的方法是什么?我显然想避免编写100 NSMutableArrays并手动完成所有操作。
作为参考,当记录结束时,用户选择他要导出的组(5个通道),然后根据该数量,另一个for循环将通过NSMutableCombinationArray并导出最终的数组。
最佳答案
编写包装器类:
@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