0

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];
}
2
  • 1
    whatever you wrote, do same thing in reverse order by forming objects. Commented Apr 9, 2014 at 11:49
  • Yes, just what @AnoopVaidya said: create a new class to hold your data and maintain an array of instances of that class. Commented Apr 9, 2014 at 11:50

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your quick answer. When I use addObjectsFromArray the two arrays are the same, but I want the second one to be three dimensional, do you know how to do this?
Hi User32, as mentioned, I encourage you to read some basic tutorials on NSArray for Xcode. There are 100s of great QA on here. I also encourage you to vote-up or tick the useful answers on here; until you get some "points" it's hard to have people answer question. And many people just won't answer questions from people who, previously, have failed to tick and vote up answers. Good luck!
0

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.

1 Comment

Oh I see, but how can I do it then, do you have any idea?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.