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 🙏

3
  • See stackoverflow.com/a/24069331/1630618 Commented Jun 4, 2022 at 18:18
  • In your case: if let idx = number.firstIndex(of: numberText) { print(idx) } Commented Jun 4, 2022 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 Commented Jun 4, 2022 at 23:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.