How do I convert this for block to swift 3
let row = textField.tag
if row >= arrayOfLinks.count {
for var addRow = arrayOfLinks.count; addRow <= row; addRow += 1 {
arrayOfLinks.append("")
}
}
Thanks
How do I convert this for block to swift 3
let row = textField.tag
if row >= arrayOfLinks.count {
for var addRow = arrayOfLinks.count; addRow <= row; addRow += 1 {
arrayOfLinks.append("")
}
}
Thanks
A C-style for loop is not needed at all
let row = textField.tag
while arrayOfLinks.count <= row {
arrayOfLinks.append("")
}
Try this:
let row = textField.tag
if row >= arrayOfLinks.count {
var addRow = arrayOfLinks.count
while addRow <= row {
arrayOfLinks.append("")
addRow += 1
}
}
Since you repeatedly append the same (value) element, you needn't use a loop here, but can simply append a collection to the existing arrayOfLinks
array:
if arrayOfLinks.count <= row {
arrayOfLinks.append(contentsOf:
[String](repeating: "", count: row - arrayOfLinks.count + 1))
}
This should also be more performant than repeatedly appending (same value) elements, not that this should be an issue, however.
Note also that (as per your original solution), a row
value of e.g. 10
will yield a total (existing + new) of 11
elements in the numberOfEntries
array.