Home > database >  How to create a Tuple from an Array String
How to create a Tuple from an Array String

Time:10-02

I have String Array ["14 159", "14 163", "12 154", "14 160", "13 158", "14 161"] and I wonder how to create tuple from it? It should be [(14, 159), (14, 163) .. etc]

CodePudding user response:

Normaly this should work:

import Foundation
var list1 = ["14 159", "14 163", "12 154", "14 160", "13 158", "14 161"]
var list2 : [(Int, Int)] = []
for items in list1{
    var turple = (0 , 0)
    let step1 = items.components(separatedBy: " ")
    turple.0 = (step1[0] as NSString).integerValue
    turple.1 = (step1[1] as NSString).integerValue
    list2.append(turple)
}

print(list2)

  • Related