Problem: Array of String Data -> Array of Double (and Int) Data

This may be trivial to some but I just would like to know if you can copy an array to a new array and change the type of the data for each column of elements?

I have loaded a file into an array as String data as I can't seem to do it any other way (i.e. as another type) I now want to convert some of the columns to a different type in possibly a new array.

I have an array full of string data and I would like to convert it to Double, or some Int, data so that I can easily do my calculations.

I would appreciate as much info as possible as I am relatively new to SwiftUI and Xcode.

Many thanks,

D Norris

If I understand correctly your question, it is easy using map.

Something like this to get Int or Double

let myStringArray: [String] = ["1", "2", "3"]
let myIntArray = myStringArray.map { Int($0) ?? 0 }
let myDoubleArray = myStringArray.map { Double($0) ?? 0 }

You need ?? as Int($0) returns an optional that could be nil if item in myStringArray does not represent an Int or Double value.

You get:

[1, 2, 3]
[1.0, 2.0, 3.0]
Problem: Array of String Data -> Array of Double (and Int) Data
 
 
Q