0

I have an array which contains objects. These objects may have other arrays of objects, then those objects also may have other objects and array of objects. There is no limit really.

So how do I go through all the objects in this array no matter how deeply nested they are? If I know how deep the arrays go, I can easily do this by using a number of for loops. I having problem with going through all the elements.

I am not able to come up with a logic to do that. Can somebody please help me? I am trying to do this in swift.

1
  • If you don't know how deep your array can be, you are in real trouble. You need to rethink your design. And if you don't help yourself, no one will be able to help you here. When you make an effort, you can share your code here to get better response. Commented Jul 19, 2018 at 7:23

1 Answer 1

1
  • Have an Any object.

  • Store the values into that object how you need it.

  • When you read the object, cast it to the required array.

  • If the casting is successfull, then you can access that Any object like an array.

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

2 Comments

But how do I go through each elements without knowing how deep it is? I mean if it was 2 levels deep I could have put 3 for loops. But in this case I don't have any idea. I know how to access the items without issue. Sorry if I was no clear. Updating the question now.
Use recursion. Have a method to which you pass this Any object. If casting to an array is successful, call that method individually to every element of that array.

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.