I\'d like to know how can I convert a String in an Int array in Swift. In Java I\'ve always done it like this:
String myString = \"123456789\";
int[] myArray = n
String
into separate String
instances using:
components(separatedBy separator: String) -> [String]
Reference: Returns an array containing substrings from the String that have been divided by a given separator.
flatMap
Array
method to bypass the nil
coalescing while converting to Int
Reference: Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.
Implementation
let string = "123456789"
let intArray = string.components(separatedBy: "").flatMap { Int($0) }