Question

I want to add an Array which contains a Dictionary to an other Array. So the first Array contains a few items and each item consists of 4 key/value pairs. Now I want to add the first Array to the second on position [i,1] (so the second Array would be a three dimensional array?). How can I achieve this?

here's my code:

NSMutableArray *routenArray = [[NSMutableArray alloc] init];
NSMutableArray *firstArray = [[NSMutableArray alloc] init];

NSDictionary *firstDict = [NSDictionary dictionaryWithObjectsAndKeys:abfahrt,@"Abfahrt", ankunft, @"Ankunft", dauer, @"Dauer", route, @"Route", nil];  
[firstArray addObject:firstDict];

for (int i = 0; i < firstArray.count; i++) {
    routenArray [i][1] = firstArray [i];
}
Was it helpful?

Solution

You know, you really need to learn the basics of "3D" arrays, and indeed arrays / NSMutableArrays generally.

Fundamentally you use addObject to add an item to an NSMutableArray.

The answer to your question is that you use addObject to add objects to your "outside" array.

The things you add, can themselves me complex data objects like arrays - that's the "3D' part as you're thinking about it.

To access elements of an NSArray (or NSMutableArray), you use firstObject, lastObject, or objectAtIndex.

(You don't use braces[1][17].)

I encourage you to read some basic tutorials on NSArray for Xcode.

Click the "tick" sign to mark my answer as correct, closing this question. Then, forget you ever asked this question :) Read some tutorials, then ask new specific questions about NSArray. Also search here for 1000s of examples and QA.


It's possible you're looking for addObjectsFromArray

Generally to solve problems like this.

Go to the dock for the highest class involved in your problem, in this case NSMutableArray ...

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

no matter how experienced you are, you can always find amazing stuff in there. Just read through each method available to you; often you find just what you want.

In this case addObjectsFromArray is your gift from above. Next, on this site just search on addObjectsFromArray and you'll find lots of great example code and other incidentalia.

Cheers!

OTHER TIPS

There are no "three dimensional arrays". There is NSArray (and NSMutableArray), and they contain objects. Since NSArray and NSDictionary are themselves objects, an NSArray can contain another NSArray which can contain another NSArray. That doesn't make it three-dimensional. And that's where your problem comes from: "routenArray" (wer mixt hier deutsche und englische Namen? ) doesn't contain any objects. routenArray [0] is nil.

What you are writing:

routenArray [i][1] = firstArray [i];

is just a shortcut for

[[routenArray objectAtIndex:i] replaceObjectAtIndex:1 withObject:[firstArray objectAtIndex:i]];

So this is very much not what you think it is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top