Answer the question
In order to leave comments, you need to log in
How to transfer an array from Int -> String in a function using Switch?
If given a number between 0 and 9, return it in words.
Entry :: 1
Exit :: "One".
Initial function:
func switchItUp(_ number: Int) -> String {
}
func switchItUp(_ number: Int) -> String {
let array = [1...9]
switch _ ((Int) -> String) {
case [1]: print("One")
case 2: print("Two")
break
default: break
}
}
Answer the question
In order to leave comments, you need to log in
What does the line do:
I did not understand about the translation into an array. In the code example, you are simply outputting the values. If I misunderstood your question, please clarify.
But never mind, let's move on to crutches .
We do it through a function:
func switchUp(_ value: Int) -> String? {
guard value > 0 && value < 10 else { return nil }
switch value {
case 1:
return "Один"
case 2:
return "Два"
default:
return nil
}
}
enum LocalizedValue: String {
case 1 = "Один"
case 2 = "Два"
case 3 = "Три"
}
let string = LocalizedValue.1.rawValue
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question