-3

for instance I have an array of Int:

let digits = [Int](0...9)

Can I convert this to array of Characters: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]?

Swift doesn't implicitly convert types so this doesn't work:

let digits: Array<Character> = [Int](0...9)

Note: only 0-9 valid, digits could be unsorted.

Benj
  • 736
  • 7
  • 21
Volodymyr
  • 1,192
  • 21
  • 42
  • why you downvote this question, please describe what's wrong with it? – Volodymyr Dec 24 '17 at 22:06
  • 6
    I didn't down-vote, but it means that someone concluded that "This question does not show any research effort" or that "it is unclear or not useful". You can forestall down-votes in the future by demonstrating what research you've done. Also, this is a tad unclear (is it the converting from `Int` to `Character` that you had a problem with or the how to do repeat this across the array of `Int`?). But, I'm with you, that I wish people were more forthcoming regarding their specific concerns... – Rob Dec 24 '17 at 23:10

5 Answers5

4
func convertArray(array: [Int]) -> [Character] {
    return array.map {
        Character(String($0))
    }
}
yakovlevvl
  • 524
  • 3
  • 12
  • Note that this would be limited to single digit Array of integers. So this will crash if you pass integers above 9 and below 0 – Leo Dabus Dec 24 '17 at 22:34
2

Try this:

Array(0...9).map({String($0)}).map({ Character($0) })

In the code above, we are taking each Int from [Int], transform it into String using the String constructor/initializer (in order words we're applying the String initializer (a function that takes something and returns a string) to your [Int] using map an higher order function), once the first operation is over we'd get [String], the second operation uses this new [String] and transform it to [Character].

if you wish to read more on string visit here.

@LeoDabus proposes the following:

 Array(0...9).map(String.init).map(Character.init)  //<-- no need for closure

Or instead of having two operations just like we did earlier, you can do it with a single iteration.

Array(0...9).map({Character("\($0)")})

@Alexander proposes the following

Array((0...9).lazy.map{ String($0) }.map{ Character($0) })

(0...9).map{ Character(String($0)) } //<-- where you don't need an array, you'd use your range right away
Lamour
  • 3,002
  • 2
  • 16
  • 28
  • 2
    `map(String.init).map(Character.init)` or with a single iteration `map({Character("\($0)")})` – Leo Dabus Dec 24 '17 at 22:02
  • oh ok @LeoDabus – Lamour Dec 24 '17 at 22:05
  • 6
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Dec 24 '17 at 22:26
  • if you pass integers above 9 and below 0 - Fatal error: Can't form a Character from a String containing more than one extended grapheme cluster – yakovlevvl Dec 24 '17 at 22:51
  • So this answer isn't better than mine. Moreover my code is shorter. – yakovlevvl Dec 24 '17 at 22:54
  • There's no need (and it is wasteful) to make an array out of the range `0...9`. You can just do: `(0...9).map{ String($0) }.map{ Character($0) }` – Alexander Dec 24 '17 at 23:45
  • Better yet: `Array((0...9).lazy.map{ String($0) }.map{ Character($0) })`, or just: `(0...9).map{ Character(String($0)) }` – Alexander Dec 24 '17 at 23:46
  • @LeoDabus I've added your code to the answer and give you credit for it – Lamour Dec 25 '17 at 00:41
  • @Alexander I've added your code to the answer and give you credit for it – Lamour Dec 25 '17 at 00:41
  • @RosárioPereiraFernandes yes I was going to add some explanations but something came up – Lamour Dec 25 '17 at 00:42
  • I wrote about "Fatal error" because @LeoDabus said "Note that this would be limited to single digit Array of integers. So this will crash if you pass integers above 9 and below 0" under my answer, and after that my answer was unchecked and answer of Lamar was checked as right. But Lamar answer is not better than mine, it is even worse because code has two maps (it is longer). Now i see that Lamar added code of LeoDabus and Alexander and didn't add mine. Probably, this is because Lamar is offended that my answer was initially marked as correct and that I wrote a comment about Fatal error. – yakovlevvl Dec 25 '17 at 01:21
  • @ascentman please read my comment and decide whose answer should be marked. – yakovlevvl Dec 25 '17 at 01:24
  • @yakovlevvl I am and was not offended, I don't see the need of restating your answer since it was not a `comment` under any posts. Happy Holiday – Lamour Dec 25 '17 at 15:45
1

Try this

 var arrChars = [string]()

 for i in 0..<digits.count
 {

    let item = digits[i]

    arrChars.append("\(item)")

 }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

Just use the map function:

let stringArray = digits.map{String($0)}
Christian
  • 22,585
  • 9
  • 80
  • 106
  • This isn't an array of characters. Btw you could just pass the String initializer to map. No closure needed. `map(String.init)` – Leo Dabus Dec 24 '17 at 21:57
0

One possible way is to create the Character from Ascii codes.

let charArrFromAscii = Array(48...57).map({ Character(UnicodeScalar($0)) })

Another way is to map the Int value to a Character.

let charArrFromInt = Array(0...9).map({ Character("\($0)") })
Yannick
  • 3,210
  • 1
  • 21
  • 30