Last active
August 22, 2021 19:03
-
-
Save deathlezz/5e9b7d0349d405d609e6b51a1d125c5e to your computer and use it in GitHub Desktop.
Convert Int array to String array and vice versa in Swift 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Convert Int array to String array and vice versa | |
// | |
// Int array -> String array | |
let intArray = [1, 2, 3, 4, 5] | |
let stringArray = intArray.map { String($0) } | |
// or "let stringArray = intArray.map(String.init) | |
print(stringArray) // ["1", "2", "3", "4", "5"] | |
// String array -> Int array | |
let stringArr = ["1", "2", "3", "4", "5"] | |
let intArr = stringArr.map { Int($0)! } | |
// or "let intArr = stringArr.compactMap(Int.init) | |
print(intArr) // [1, 2, 3, 4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment