swift - Replace elements of 2 arrays? -


i have 2 arrays: var underarray = ["_", "_", "_", "_", "_"] , var letterarray = ["a", "b", "c", "d"]. have button, , every time press button want replace element of underarray 1 letterarray.

for exmple:

  • first press: var underarray = [a, _, _, _, _]
  • second press: var underarray = [a, b, _, _, _]
  • third press: var underarray = [a, b, c, _, _] etc...

i can manually like: underarray[0] = letterarray[0] , underarray[1] = letterarray[1], that's not option.


so far tried creating - loop , did not work:

     @ibaction func mybuttons(sender: uibutton) {       var index = 0; index < underarray.count; ++index {     swap(&underarray[index], & letterarray[index])     }  } 

i suspect it's wrong approach. right approach?

you have declare variables out of ibaction , make sure don't in case button has been pressed many times:

var presscounter = 0 var underarray = ["_", "_", "_", "_", "_"] var letterarray = ["a", "b", "c", "d", "e"]  @ibaction func mybuttons(sender: uibutton) {      if presscounter<underarray.count {         swap(&underarray[presscounter], &letterarray[presscounter])     }     // lets increment +1 (the button has been pressed)     presscounter++ } 

Popular posts from this blog