1

We can use an array [String] to get the number text from its number.

let number = ["One", "Two", "Three", "Four", "Five"]

e.g. number[0] means "One"

How do I get a simple order number of the text in the array having just its text ("Five" etc.) using Dictionary?

I tried a function like that:

let number = ["One", "Two", "Three", "Four", "Five"]
func getNumber(_ numberText: String) {
    if numberText == "One" {
        print(0)}
    if numberText == "Two" {
        print(1)}
    if numberText == "Three" {
        print(2)}
    if numberText == "Four" {
        print(3)}
    if numberText == "Five" {
        print(4)}
}

getNumber("Two")

But it has nothing common with the initial array at the top. Please help guys

  • See https://stackoverflow.com/a/24069331/1630618 – vacawama Jun 04 '22 at 18:18
  • In your case: `if let idx = number.firstIndex(of: numberText) { print(idx) }` – vacawama Jun 04 '22 at 18:20
  • Not sure what is your goal but it would be much simpler to decode the string text with `NumberFormatter` using `spellOut` `numberStyle`. [Numbers name Text to Int](https://stackoverflow.com/a/46856525/2303865) – Leo Dabus Jun 04 '22 at 23:36

0 Answers0