简体   繁体   中英

How to save two Arrays in two dimensional Array?

I am new at iOS Dev. I want to save two different arrays ( array1 & array2 ) in 2 dimensional array. I know how to save data directly in two dimensional array but can't by save two different arrays in one.

NSString* path = [[NSBundle mainBundle] pathForResource:@"Aasvogel" ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

NSArray* foo = [content componentsSeparatedByString: @","];

NSMutableArray *array1 = @[], *array2 = @[];
for ( int i = 0; i < [foo count]; i++ )
{
    NSString* day = foo[i];
    if ( i % 2 == 0 ) { [array1 addObject:day];}
    else { [array2 addObject:day];}
}
// and here i have populated two arrays (array1 and array2)
// Now i want to save these arraya in below two dimensional array (dataArray) atIndex:0 and at Index:1


NSMutableArray *dataArray = [[NSMutableArray alloc] initWithCapacity: 2];
[dataArray addObject:[NSMutableArray arrayWithObjects:@"e",
                                                         @"el",
                                                         @"ale",
                                                         @"vela",
                                                         @"gavel",nil] atIndex:0];

[dataArray addObject:[NSMutableArray arrayWithObjects:@"Represents 50 in Roman numeral",
                                                         @"Building Wing",
                                                         @"Pub Brew",
                                                         @"Thin Parchment or membranes",
                                                         @"chairperson's hammer",nil] atIndex:1];

I have recently implemented 2D array into my application. Please check below code which is available at 2DArray

int capacity;
@property (nonatomic, strong) NSMutableArray *outerArray;

#define kCRL2DArrayEmptyKey @"kCRL2DArrayEmptyKey"

- (id) initWithRows:(int)x columns:(int)y
{
    if (self = [super init])
    {
        capacity = y;
        self.outerArray = [NSMutableArray array];
        for (int i = 0; i < x; i++) {
            NSMutableArray *innerArray = [NSMutableArray array];
            for (int j = 0; j < y; j++) {
                [innerArray addObject:kCRL2DArrayEmptyKey];
            }
            [self.outerArray addObject:innerArray];
        }
    }
    return self;
}

I am not sure about 2-dimensional array in iOS but if I were you I would be saved the two arrays within a dictionary such as

 NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
 [dict setvalue:yourArray forKey:@"FirstArray"];
 [dict setvalue:yourSecondArray forKey:@"SecondArray"];

And Use it accordingly.

There's no such thing as a two (or more) dimensional NSArray . If you genuinely need an n -dimensional array object in iOS or OS X, you can of course roll your own, or you could instead create an NSArray of NSArray instances (which are columns and which are rows is entirely up to you). In that case, you could eg add items by doing

[[outerArray objectAtIndex:0] addObject:@"Foo"];
[[outerArray objectAtIndex:1] addObject:@"Bar"];

That said, for the problem you are tackling, it looks to me as if an NSDictionary might be more appropriate, eg with keys @"e", @"el" and values @"Represents 50 in Roman numerals", @"Building Wing" .

If your concern is that the keys of NSDictionary are not held in sorted order, you can always extract the keys as an array and sort them. Or, if the keys change regularly, you might want to use a more sophisticated approach (eg keeping a separate sorted array of keys and inserting them into the right place when adding to the NSDictionary ).

Also, you know that in modern Objective-C you can write eg

@[ @"a", @"b", @"c" ]

or

@{ @"a": @1, @"b": 2 }

rather than the very verbose forms you're using above?

this is how u add anything in a 2d array ie an Array of arrays in objective-c

NSMutableArray *array 1 = [[NSMutableArray alloc]init];
NSMutableArray *array 2;

for(int col = 0;col <5;col++){

       array2 = [[NSMutableArray alloc]init];
         for(int row = 0;row<5;row++){

              [array2 addObject:myItems];

    }
       [array1 addObject:array2];

}

hope this helps

you can try this

NSArray * firstArray, *secondArray;
NSArray * mainArray= [[NSArray alloc]initWithObjects: firstArray, secondArray, nil];

use for loop to generate 2d array from 2 different array, follow this stracture

int i, j;
for(i = 0; i < nrows; i++)
    {
    for(j = 0; j < ncolumns; j++)
        array[i][j] = 0;
    }
}

May be it will help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM