简体   繁体   English

需要遍历另一个数组内部的数组

[英]Need to loop through an array that is inside of another array

I have an array that contains 2 objects. 我有一个包含2个对象的数组。 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. 我知道如何进行普通的for循环并遍历一个数组,但是我从来没有像这样需要在另一个数组内部循环遍历一个数组。

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. 我认为我需要做的是技术上称为“遍历嵌套数组”,但我似乎找不到与Objective-C一起做的任何事情。

Thanks for the help. 谢谢您的帮助。

Use a nested for loop and you can iterate through the objects in both arrays: 使用嵌套的for循环,您可以遍历两个数组中的对象:

for(NSArray* array in arrays){
  for(object* thing in array){
   //do what you want with thing in arrays
  }
}

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. 如果您需要遍历两个数组中的所有对象,则@JMarsh的代码将执行此操作。

If you only need to fetch the inner array, then just use an explicit fetch Following JMarsh's format: 如果只需要获取内部数组,则只需按照JMarsh的格式使用显式获取:

NSArray *innerArray = arrays[1];  //Or whatever array index is correct
for(id thing in innerArray)
{
  //do what you want with thing
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM