swift - Iterate Array 30 times using For loop -
i have array 8 elements want copy elements 1 one until count of new array reach 30 times.
i used loop , while result copying first element 30 times.
let myshift = ["d12","e","n12","n","off","rest1","rest2","d"] var myarray = [string]() in myshift { while myarray.count != 30 { myarray.append(i) }}
i read examples map tried use didn't work.
since it's not clear looking for, here 2 options.
this 1 repeat contents of array until reaches 30 items in total. keep order , loop them. results in array 30 elements.
let myshift = ["d12","e","n12","n","off","rest1","rest2","d"] var myarray = [string]() (0..<30).foreach { myarray.append(myshift[$0 % myshift.count]) }
this 1 result in array each element repeated 30 times, total of 240 elements.
let myshift = ["d12","e","n12","n","off","rest1","rest2","d"] let array = [[string]](count: 30, repeatedvalue: myshift).flatmap { $0 }
Comments
Post a Comment