Here is a piece of code.

@IBAction func itemsHidden(sender: UIButton!) { if items.count > 0 && aiDeciding == false { sender.hidden = true if let index = items.indexOf(sender) { items.removeAtIndex(index) print("index is:",(index)) } } } 

Total objects 21 - (@ IBOutlet var items: [UIButton]!). When you click on an object, it disappears from the screen. Then we delete its index. But when the index is displayed, the values ​​are repeated. Please explain why this happens?

  • in the sense that if you delete, for example, an object with an index of 10, is there still an object with an index of 10 in the array? - Max Mikheyenko 9:26 pm
  • I take the first three objects. I was printed on the second and third objects. index is 4 .... Shouldn't they be different? - Spartak
  • ahhh I suppose that after deleting the 4th object, the 5th object moved and became 4m, and then it was deleted, but already as the 4th. - Max Mikheyenko 9:31 pm
  • Hmmm ... rather, it is .... Tell me, please, how can I assign a unique value to sender then? - Spartak
  • UIButton has a tag property, you can save to it (just start counting from 1, because all objects have a default of 0 - you may get confused), or make your UIButton subclass and write any property you like - Max Mikheyenko

1 answer 1

You delete an object by index. The index itself (normal int) will not change. But the items.indexOf (sender) function will produce a new result (like NSNotFound) since the object is deleted.