Remove element from collection during iteration with forEach

前端 未结 3 1602
旧巷少年郎
旧巷少年郎 2020-12-06 09:30

Recently, I wrote this code without thinking about it very much:

myObject.myCollection.forEach { myObject.removeItem($0) }

where myOb

3条回答
  •  旧巷少年郎
    2020-12-06 10:08

    The Collection was copied before starting the iteration , and the code inside Foreach is applied on the real collection but the iteration is happening on the copied Collection which will be deleted after the last iteration .

提交回复
热议问题