How to convert string to integer

I have array of string, how can i convert that array of string to array of integer. For converting to int im tried

1. var intArry = arr.flatMap{Int($0)}


2. var intArry = arr.map{Int($0)}, 3. var intarry = Int(arr) and etc... but im did't get the array of Int. if im using some code im getting [0,0,0,0,0,] and some times getting empty [ ].

Replies

First of all, Printing is not an appropriate place to put this sort of article. Better visit Swift,


You should better include:

- Some examples of input data

- Desired output for each example


Assuming you have an Array of Strings, where all elements represent a valid integer:


let strArray = ["1", "2", "3"]
let intArray = strArray.map {Int($0)!} //<- Do not miss `!`.
print(intArray) //->[1, 2, 3]


If your Array may contain some non-integer Strings, you need to define the behavior for such values.