Zaphod
Zaphod

Reputation: 7270

Swift 2 if case syntax with enum having associated values

I'm stuck with some really simple syntax I guess, but I can't find how to solve it.

And I was wondering if the if casesyntax could be of any help...

But I could not find it (may be the tiredness, I hope...)

I'm looking for something like that:

if case result["ABC"] == .City(data:let data, position:let position) { 
    // Do something with data and position
}

So I'm sure it's obvious, but I've missed it... So if you can help, that would be great.

Thanks in advance.

Upvotes: 4

Views: 1198

Answers (1)

Wallace Campos
Wallace Campos

Reputation: 1301

Swift dictionaries return optional values. So, using switch, you should do:

switch result["ABC"] {
case let .City(data, position)?:
   // Do something with data and position
default:
  break
}

Using if pattern matching:

if case let .City(data, position)? = results["ABC"] {
  // Do something with data and position
}

Upvotes: 13

Related Questions