3

I want to use a variable-sized multi-dimensional array in my app to save data. The data structure I want to use is as below, the first element in each row is followed by corresponding multiple values.

 array = {   {a, a_val1, a_val2, a_val3}.  
             {b, b_val1},  
             {c, c_val1, c_val2, c_val3, c_val4, c_val5}  
         }

Any idea how I can implement in objective-c?

3 Answers 3

15

use NSMutableArray like so

NSMutableArray *curRow; /* use to access the row while loading with objects */
NSMutableArray *array = [[NSMutableArray alloc] init]; /* your main multidim array */
curRow = [NSMutableArray array];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[array addObject:curRow]; /* first row is added */

/* rinse and repeat */
curRow = [NSMutableArray array];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[curRow addObject:/* what you want here */];
[array addObject:curRow];
4
  • Thank you - this is perfect. I am going to mark this as my answer because it offers a generic solution - for creating a dynamic array at run time. The next answer shows usage for a fixed size array.
    – Sam
    Sep 18, 2011 at 14:57
  • This answer is so much better Jan 7, 2014 at 3:34
  • To call a specific object, you can call the row it's on (the first set of "cur row" would be 0, the second set is 1, etc.) Then call the "addObject" (column) number it is... the first "addObject" is "0", the second is "1", etc. [[array objectAtIndex:/*Row Number*/] objectAtIndex:/*column number*/]; Mar 31, 2014 at 17:00
  • [(NSMutableArray *)[array objectAtIndex:/*X*/] objectAtIndex:/*Y*/]; Mar 31, 2014 at 17:36
5

use NSMutableArray

Below is the example for your understanding ...

NSMutableArray * multiArray = [[NSMutableArray alloc] initWithCapacity:5];
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2]];
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2,a_val3,a_val4]];
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val5]];
[multiArray addObject:[NSArray arrayWithObjects:a,a_val1,a_val2,a_val3,a_val4,a_val5,a_val6]];

And Don't forget to release to multiArray array because we have alloced it ...

3
  • Thanks for the answer....but I was looking for idea for creating a dynamic multi-dimension array at runtime. The prior answer was perfect for that.
    – Sam
    Sep 18, 2011 at 14:58
  • @Sam Why do you think this method can't also be used at runtime? Mar 31, 2014 at 16:58
  • @AlbertRenshaw, this answer is similar to bigkm's, and it could be used at runtime. However, it requires that you have the subarray's objects available to add in advance (arrayWithObjects:...). If, say, you don't know how many objects you will have in a given subarray initially, then bigkm's answer allows the objects to be created and added 'on-the-fly'.
    – leanne
    Apr 25, 2014 at 14:27
-1

Objective-C does not have a real 2 dimensional array type but you can implement it with the following codes..

in your header file --- yourheader.h

#import <Foundation/Foundation.h>

@interface yourheader : NSObject{

    NSMutableDictionary* DictionaryArrayType;
    NSMutableArray* MultiArrayType;
    NSArray* CaptionTitle;
    NSArray* ObjectValue;

}

@property (strong, nonatomic) NSMutableDictionary* DictionaryArrayType;

@property (strong, nonatomic) NSArray* CaptionTitle;

@property (strong, nonatomic) NSArray* ObjectValue;

@property (strong, nonatomic) NSMutableArray* MultiArrayType;

-(id) AddArrayObjects:(NSString*)_Name : (NSString*)_Surname :(NSString*)_Age;

-(id) AddArrayDictionaryObject:(NSArray*)_ArrayObject : (NSArray*)_ArrayKey;

-(id) AddMultiArrayType:(id)_ArrayObject;

-(void) ShowMultiArrayType:(id)_ArrayObject;

@end

Now add to your objective-c file ---- objective-c.m

#import "yourheader.h"

@implimentation yourheader

@synthesize DictionaryArrayType;

@synthesize CaptionTitle;

@synthesize ObjectValue;

@synthesize MultiArrayType;


-(id)init {

    if(self = [super init]){

        NSString* const NAME = @"NAME";
        NSString* const SURNAME = @"SURNAME";
        NSString* const AGE = @"AGE";
        //Adding fixed content to CaptionTitle Array
        [self setCaptionTitle:[NSArray arrayWithObjects:NAME, SURNAME, AGE, nil]];

        //add values to ObjectValue array
        [self AddArrayObjects:@"Bob" :@"Obi" :@"200"];

        //add values to dictionary
        [self AddDictionaryArrayType:ObjectValue :CaptionTitle];

        //Add to the Multi dimensional array [][]
        [self AddMultiArrayType:DictionaryArrayType];  

        //add the second row values to ObjectValue array
        [self AddArrayObjects:@"Barack" :@"Obama" :@"50"]; 

        //add values to dictionary
        [self AddDictionaryArrayType:ObjectValue :CaptionTitle];

        //Add to the Multi dimensional array [][]
        [self AddMultiArrayType:DictionaryArrayType];  

        //display the 2d Array
        [self ShowMultiArrayType:MultiArrayType];

    }

    return self;
}


-(id)AddArrayObjects:(NSString *)_name :(NSString *)_surname :(NSString *)_age {

    //Set the Array Objects;
    [self setObjectValue:[NSArray arrayWithObjects:_name, _surname, _age, nil]];

    return self;

}

-(id)AddDictionaryArrayType:(NSArray *)_ArrayObject :(NSArray*)_ArrayKey {

    if(!DictionaryArrayType) {
        //initialize disctionary
        [self setDictionaryArrayType:[NSMutableDictionary dictionary]];
    }
    //add array obeject and Fixed Key decleared in CaptionTitle array
    DictionaryArrayType = [NSMutableDictionary dictionaryWithObjects:_ArrayObject forKeys:_ArrayKey];
    return self;
}

-(id) AddMultiArrayType:(id)_ArrayObject {

    if(!MultiArrayType) {

        [self setMultiArrayType:[NSMutableArray array]];
    }

    [MultiArrayType addObject:_ArrayObject]; 

    return self;
}

-(void)ShowMultiArrayType:_ArrayObject {

    for(id objects in _ArrayObject ) {

        for(id key in objects) {
            NSLog(@"%@ key = : object =  %@", key, [objects objectForKey:key]);

        }
    }
}


@end;

To finish add this to your appdelegate.m file inside the app

  yourclassname* _yourclasspointer = [[yourclassname alloc] init];

 [_youclasspointer ShowMultiArrayType:[_yourclasspointer MultiArrayType]];

You should see it in you console.

1
  • 1
    Your solution ignores objective c language conventions such as beginning property names with a lowercase letter, naming all your method parameters and always using getter and setter methods for declared properties
    – jackslash
    May 6, 2013 at 17:06

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.