2

How can i convert an range int array into an string array?

Like this range int array:

let data = [Array(1...9), Array(0...59), Array(0...59)]

To an string array like:

let data = [["1", "2", "3",.....], ["0","1", "2", "3",...], ["0","1", "2", "3",...]]

3 Answers 3

3

Try this

data.map{ $0.map(String.init) }
Sign up to request clarification or add additional context in comments.

3 Comments

Nice solution! Very concise.
Can you explain what String.init mean. Why String.init() cannot run?
map function takes Int->SomeType as a parameter. So, I pass String.init, which is a String initilizer. It's like when you call String(123), you can also call String.init(123). Finally, since String.init has many signatures, Swift Compiler automatically infer the type to Int->String
0
let data = [Array(1...9), Array(0...59), Array(0...59)]

var stringArray = Array<Array<String>>()

for array in data {
    var subArray = Array<String>()

    for item in array {
        subArray.append(String(item))
    }

    stringArray.append(subArray)
}

Comments

-2

You can try like this when you get the value from array.

Code Snippets:

let num = [1, 2, 3]
let data = [num, num, num]

let value = data[0]

for x in value {
    print(String(x))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.