Swift array to array of tuples
Use zip
and map
:
let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let yaxis = [1, 2, 3, 4, 5]
let tuples = zip(xaxis, yaxis).map { ($0, $1) }
Try this:
let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let yaxis = [1, 2, 3, 4, 5]
var newArr = [(String, Int)]()
for i in 0..<xaxis.count {
newArr.append((xaxis[i], yaxis[i]))
}
print(newArr)
Try this:
let arrayMerged = zip(xaxis, yaxis).map { ($0, $1) }
or this:
let arrayMerged = Array(zip(xaxis, yaxis))