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.

1

2 Answers 2

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
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

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".
Well, those are just placeholders. What kind of data is being returned? Strings, numbers, etc.?
The actual object being returned is an array that contains an array. The contained array that I want to access contains NSString objects.
Then replace object* with NSString* and change thing to whatever describes the array's data best.
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: stackoverflow.com/questions/22123921/…
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
}

1 Comment

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: stackoverflow.com/questions/22123921/…

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.