我是iOS Dev的新手。我想在2維數組中保存兩個不同的陣列(array1
& array2
)。我知道如何直接將數據保存在二維數組中,但不能將兩個不同的數組保存在一個數組中。如何在二維數組中保存兩個數組?
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];
兩個數組是不夠的,建立一個二維數組。即'array1 + array2'不是'array1 * array2'。或者它只是'[dataArray addObject:array1]; [dataArray addObject:array2];'?不太清楚... – trojanfoe
@trojanfoe但我使用它作爲硬編碼,因爲你可以看到我分配dataArray的代碼的第二部分,我用值填充了數組。 –
可能的重複: http://stackoverflow.com/questions/10527521/how-to-produce-2-dimensional-array-in-objective-c http://stackoverflow.com/questions/638129/how-to -declare-a-two-dimensional-array-of-string-type-in-objective-c http://stackoverflow.com/questions/4362740/creating-a-two-dimensional-array-in-objective-c – Szu