0

Ok, I basically need to find a way to better manage 100 NSMutableArrays of data.

Like, (I haven't actually done this) :

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

I need something that has multiple facets, like:

NSMutableCombinationArray *theMassiveArray;

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

In the end I want to do something like this:

//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.

So there are 512 channels of DMX, and each array needs to hold 5 channels. It's a recording app that basically stores 500 DMX channel values at a resolution of one time a second.

How can I do this? What's the best way to do this? I obviously want to avoid writing 100 NSMutableArrays and doing this all manually.

For reference, when recording is over, the user selects how many groups (of 5 channels) he wants to export, then depending on that number, another for loop will go through the NSMutableCombinationArray and export the final arrays.

2
  • One suggestion would be to simply add them all to a dictionary. Then, you can assign a nice human readable key to each of the arrays. if you need to change a value in an array, simply pull it out using its key, make your change. Or, make a custom object that does exactly what you want. You're a programmer afterall!
    – Jeremy
    Feb 6, 2013 at 21:22
  • In that case, I'd need a key for each line of DMX? Or can you add data to an object at a specific key instead of writing over it? Feb 6, 2013 at 21:30

3 Answers 3

2

Write a wrapper class:

@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
7
  • I think this is the best solution, subscripting is the right way to do it
    – oiledCode
    Feb 6, 2013 at 21:48
  • Awesome! How do I access the contents of the wrapper? Feb 6, 2013 at 21:59
  • like - (void)getObjectatIndex:(NSUInteger)idx fromArrayAtIndex:(NSUInteger)arrIdx; Feb 6, 2013 at 22:00
  • @objectiveccoder001: I can't get to this right now, but I'll come back in a few hours. It's the opposite of the setter method, though: just call through to objectAtIndex: twice, for the outer and then the inner array.
    – jscs
    Feb 7, 2013 at 0:08
  • Yeah, when you have a chance, that'd be absolutely amazing :) I've been trying to mess with it, but to no avail. Feb 7, 2013 at 2:44
1

Here's what you should do

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];        
}

So finally the massiveArray contains 100 dictionaries, and for instance, to access the 2nd value of 45th dictionary, you do this :

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

Hope this helps

3
  • 1
    I believe you are missing the inner array. The massiveArray should be an array or arrays that hold dictionary entries. The inner array is necessary as the dictionary needs to be inserted repeatedly to the same array (every second).
    – Joel
    Feb 6, 2013 at 21:55
  • Thanks, I've edited my answer, the loop now runs from 0 to 500, with increments of 5, and the each of the 100 dictionaries contain 5 consecutive items from the DMXChannelArray. Look right now?
    – Dhruv Goel
    Feb 6, 2013 at 22:01
  • I think you want a top level array of 100 arrays, which can each have unlimited number of dictionaries (with each dict having 5 entries). @josh's answer looks right to me for reference.
    – Joel
    Feb 6, 2013 at 22:36
0

Well

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

To retrieve your values

[dic objectForkey:[NSNumber numberWithInt:array_index][array_element_index];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.