0

I have an array that contains 2 objects. In order to store it on my backend server service, I need to store it inside of another array.

So later on, when I call my server and tell it I want the array object, it sends me a new array object that is holding my original array object.

I need to loop through the new array (that contains my original array), and then loop through all of the objects inside of my original array.

I know how to do a normal for loop and loop through an array, but I have never had to do it like this where you need to loop through an array that is contained inside of another array.

I have been thinking about ways to do this now for about an hour and really have no clue. I think what I need to do is technically called "looping through nested arrays" but I can't seem to find anything about doing this with objective-c.

Thanks for the help.

user3344977
  • 3,584
  • 4
  • 32
  • 88
  • It is called a Muliti-Dimensional Array http://stackoverflow.com/questions/6027867/seeking-multi-dimensional-array-tutorial-material – Eric Mar 02 '14 at 02:37

2 Answers2

4

Use a nested for loop and you can iterate through the objects in both arrays:

for(NSArray* array in arrays){
  for(object* thing in array){
   //do what you want with thing in arrays
  }
}
JMarsh
  • 934
  • 7
  • 19
  • the NSarray object being returned by my server is called "objects". I am trying to use your code and I replaced "arrays" with "objects", but xcode is giving me undeclared identifier warnings for both "object*" and "thing". – user3344977 Mar 02 '14 at 03:24
  • Well, those are just placeholders. What kind of data is being returned? Strings, numbers, etc.? – JMarsh Mar 02 '14 at 03:26
  • The actual object being returned is an array that contains an array. The contained array that I want to access contains NSString objects. – user3344977 Mar 02 '14 at 03:28
  • Then replace object* with `NSString*` and change `thing` to whatever describes the array's data best. – JMarsh Mar 02 '14 at 03:29
  • Thanks for the help JMarsh. I marked yours as the answer because the code does work, just not for my specific purpose. If you happen to have any experience with Parse.com please see my new question here: http://stackoverflow.com/questions/22123921/need-to-loop-through-an-nsmutablearray-that-is-stored-inside-an-array-in-parse-c – user3344977 Mar 02 '14 at 04:39
0

Do you need to loop through every object in both arrays, or do you need to fetch the object from the outer array and just loop through that?

If you need to loop through all objects in both arrays, @JMarsh 's code will do that.

If you only need to fetch the inner array, then just use an explicit fetch Following JMarsh's format:

NSArray *innerArray = arrays[1];  //Or whatever array index is correct
for(id thing in innerArray)
{
  //do what you want with thing
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • thanks for the help. This code works as well but unfortunately I'm still having issues. If you happen to have any experience using Parse.com please see my new question here: http://stackoverflow.com/questions/22123921/need-to-loop-through-an-nsmutablearray-that-is-stored-inside-an-array-in-parse-c – user3344977 Mar 02 '14 at 04:40