I have a string array with fractional numbers and decimal numbers.
let stringArray = [ "0.0", "193.16", "5/4", "503.42", "696.58", "25/16", "1082.89", "2/1"]
Each array element is mapped in a closure where numbers are extracted from the string.
let values = stringArray.map { s -> Double in
either fractional (see earlier post)
let splitStrings = s.characters.split(separator: "/").map(String.init).map({ Double($0) })
or decimal
let splitStrings = s.characters.split(separator: ".").map(String.init).map({ Double($0) })
Question: In Swift is there a way to split the string using more than one separator so a single closure can return fractional values or decimal values ?
(continuation of closure)
switch (token)) {
case "/" :
print( "fraction")
let pathA = splitString[0]!/splitString[1]!
return pathA
case "." :
print( "decimal")
let upperSplit = splitString[0]!
let lowerSplit = splitString[1]! * 0.1 // restore decimal point
let pathB = upperSplit+lowerSplit
return pathB
}
}