gpt4 book ai didi

objective-c - 将 100 个 NSMutableArray 组合成一个可控对象

转载 作者:行者123 更新时间:2023-12-03 17:35:01 25 4
gpt4 key购买 nike

好吧,我基本上需要找到一种方法来更好地管理 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个 channel ,每个阵列需要容纳5个 channel 。这是一款记录应用程序,基本上以每秒一次的分辨率存储 500 个 DMX channel 值。

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

作为引用,当录制结束时,用户选择要导出的组数(5 个 channel ),然后根据该数量,另一个 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

关于objective-c - 将 100 个 NSMutableArray 组合成一个可控对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14738954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com