S
S
Sir0zha2019-11-12 21:18:27
Swift
Sir0zha, 2019-11-12 21:18:27

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 {
}

How I tried to solve:
func switchItUp(_ number: Int) -> String {
let array = [1...9]
switch _ ((Int) -> String) {
case [1]: print("One")
case 2: print("Two")
break
default: break
}
}

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2019-11-13
@Sir0zha

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
        }
}

Another version of the crutch:
enum LocalizedValue: String {
        case 1 = "Один"
        case 2 = "Два"
        case 3 = "Три"
}
    
let string = LocalizedValue.1.rawValue

If you do not forget about localization, you can break your head (genus of nouns, number, etc.). But as I understand it, you are not for localization, but for a one-time experiment. Crutches will fit.
PS Use formatting for code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question