Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'd like to iterate through the tuple in a for loop, like this:
for i in 0...4{
       if let value = element.list_of_keywords?.i, value == keyword {
         if let value = element.name{
           returnListOfCities(name: value)
         }
       }
     }


The problem is, if I put "i" variable after "element.list_of_keywords?." I get the following error:
Quote:
type of expression is ambiguous without more context

How to iterate through tuple in a for loop? I would really appreciate your help!
Down below I provide you more detailed code:
...

let miami = City(id:1, name:"Miami", description:"A place in Florida", latitude:25.7823404, longitude:-80.3695441, list_of_keywords: ("1", "2", "3", "4", "5"))

let new_orlean = City(id:2, name:"New Orlean", description:"A place in Florida", latitude:25.7823404, longitude:-80.3695441, list_of_keywords: ("6", "7", "8", "9", "10"))


let cities = [miami, new_orlean]


func returnListOfCities(name: String) {
  for element in cities {
    if let value = element.name, value == name { 
  print(value) 
    }
  }
}

func returnListOfCities(keyword: String) {
  for element in cities {
    for i in 0...4{
        if let value = element.list_of_keywords?.i, value == keyword {
          if let value = element.name{ 
            returnListOfCities(name: value)
          }
        }
      }
    }
  }

returnListOfCities(keyword: "5")


What I have tried:

I've tried putting "i" variable created from for loop, like this:
element.list_of_keywords?.i
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900