Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在修改数组时迭代数组是否安全?_Arrays_Swift_Iteration - Fatal编程技术网

Arrays 在修改数组时迭代数组是否安全?

Arrays 在修改数组时迭代数组是否安全?,arrays,swift,iteration,Arrays,Swift,Iteration,我知道你不应该,我知道为什么。但我的意思是,一旦我试图真正思考到底发生了什么,我就不理解自己的代码 所以我有一个包含大量对象的数组。我对它进行迭代,一旦找到具有特定类型的对象,我就将其从数组中删除,并将另一个对象添加到数组中。比如说: var arr = parent.allchildren() //getting all the children in array for ele in arr{ if(ele==somethingHere){ parent.remove(el

我知道你不应该,我知道为什么。但我的意思是,一旦我试图真正思考到底发生了什么,我就不理解自己的代码

所以我有一个包含大量对象的数组。我对它进行迭代,一旦找到具有特定类型的对象,我就将其从数组中删除,并将另一个对象添加到数组中。比如说:

var arr = parent.allchildren() //getting all the children in array
for ele in arr{
   if(ele==somethingHere){
      parent.remove(ele)
      parent.add(new ele) //add new child into child array
   }
}
如果我有一个1,2,3,4,5的数组,我在迭代时删除3并添加6,实际数组将是1,2,4,5,6,但我正在迭代的数组仍然是1,2,3,4,5


我认为这很好,因为在最后我仍然得到了我想要的,它删除了元素并添加了我需要的元素。但是,在迭代时修改列表是不好的,您不应该这样做,但是对于我的情况,我认为它可以满足我的需要。在我的案例中,我看不到的潜在问题是什么?

我刚刚在操场上用以下代码进行了测试:

var arr=[嗨,再见,盖伊,弗莱,天空] 一个月{ 如果arr.count>=3{ 到达时间:2 } 版画 } 印塔 这张照片是:

hi
bye
guy
fry
sky
["hi", "bye"]

因此,当您在Swift中使用for in循环时,会复制数组,并且对其所做的更改不会影响正在迭代的数组。为了回答您的问题,只要您了解这是一种行为,那么这样做没有错。

您可能需要考虑的一件事是在迭代结束时进行所有更改。不要一个接一个地进行更改,而是在迭代时记录要进行的更改,然后在循环完成后实际进行这些更改

例如,您可以创建一个要删除的元素数组和一个要添加的元素数组

//Our array where we record what we want to add
var elementsToAdd = [Any]()

//Our array of what elements we want to remove. We record the index at 
//which we want to remove the element from the array
var indexesToRemoveAt = [Int]()

//Getting all the children in array
var arr = parent.allchildren()

//Enumerating an array allows us to access the index at which that 
//element occurs. For example, the first element's index would be 0, 
//the second element's index would be 1, the third would be 2, and so 
//on
for (index,ele) in arr.enumerated() {
  if(ele == somethingHere) {
    indexesToRemoveAt.append(index)
    elementsToAdd.append(newEle)
   }
 }

 //Now that we have recorded the changes we want to make, we could make 
 //all of the changes at once
 arr.remove(at: indexesToRemoveAt)
 arr.append(contentsOf: elementsToAdd)
请注意,删除多个索引处的数组元素需要对数组进行以下扩展。如果希望避免创建此扩展,可以始终循环遍历索引数组,并告诉数组在每个索引处删除。这个扩展函数真正做的就是在索引中循环,并删除所述索引处的数组元素

用于删除多个索引中的元素的数组扩展:

extension Array {
  //Allows us to remove at multiple indexes instead of just one
  mutating func remove(at indexes: [Int]) {
    for index in indexes.sorted(by: >) {
      if index <= count-1 {
        remove(at: index)
      }
    }
  }
}

如果不重复,则相关:。这是有效的swift代码吗?你的意思是要使用removeat:和append吗?这可能是重复的,因为它已经说过了,所以这个问题是重复的,这个答案是它的答案的重复好的,那么让我们把这个问题标记为重复问题。