the Reverend
the Reverend

Reputation: 12559

Swift Range on the fly 1...12.contains(1) Cannot invoke 'contains' with (Int)

Is is not possible to create a range and call its contains method like this:

1...12.contains(1)

When I create a var range = 1...12 and print its dynamicType I get a Swift.Range<Swift.Int>, so I'm guessing is not a type mismatch problem, or is it?

Upvotes: 0

Views: 81

Answers (3)

Qbyte
Qbyte

Reputation: 13243

Another nice way to check this: (with the pattern match operator)

if 2...5 ~= 4 {
  // do something
}

Upvotes: 2

Ivan Rigamonti
Ivan Rigamonti

Reputation: 694

place the range in parenthesis and it should work:

(1...12).contains(1)

Upvotes: 0

Chris Conover
Chris Conover

Reputation: 9039

In this case the problem was that of operator precedence:

(1...12).contains(1)   // -> true

(the code does look ambiguous otherwise)

Upvotes: 3

Related Questions