Answer the question
In order to leave comments, you need to log in
[[+content_image]]
How to turn a String array into an Int array (multidimensional)?
There is an array
[["9", "6", "7", "8", "5"], ["1", "1", "1", "1", "1"]]
, you need to get a new one
let arr = [["9", "6", "7", "8", "5"], ["1", "1", "1", "1", "1"]]
let mapArr = arr.map{Int($0).map{Int($0)}}
let arr = [[1,2,3],[4,5,6]]
let mapArr = arr.map{$0.map{$0 * 2}}
mapArr // [[2, 4, 6], [8, 10, 12]]
Answer the question
In order to leave comments, you need to log in
Good afternoon!
In your first example, you are trying to convert an array of strings to Int and you are getting an error.
If you use .map, then it returns an array of optional values. And in this case, if there is a string in the array that cannot be converted to Int, then there will be nil in this place.
If you use flatMap , then it will leave only those values that were successfully converted to Int.
let arr = [["9", "6", "7", "8", "5"], ["1", "1", "1", "1", "1"]]
let mapArr = arr.flatMap { $0.flatMap { Int($0) } }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question