Cam193
Cam193

Reputation: 1

Else/If statement help in swift

I'm getting an error when I try to use an if/else statement in Xcode 6 with Swift. This is what I have

} else if countElements(sender.text) == 1, 2

It's telling me:

Type 'String!' does not conform to protocol '_CollectionType'

How can I compare two values on one line?

Upvotes: 0

Views: 1856

Answers (3)

Antonio
Antonio

Reputation: 72750

You can also use the contains global function, providing it a range and the element to check for inclusion:

contains(1...2, countElements(sender.text))

Upvotes: 0

Mick MacCallum
Mick MacCallum

Reputation: 130193

The other answer correctly shows the way to make 2 comparisons in Swift, but this can be done in a single statement thanks to Swift's pattern matching operator. For example:

if 1...2 ~= countElements(sender.text) {
    println("count is 1 or 2")
}

Basically, the provided statement is true if the number of characters in your string is inclusively between 1 and 2.

Upvotes: 0

Leo Dabus
Leo Dabus

Reputation: 236305

You can use "||" = "or"

} else if countElements(sender.text) == 1 || countElements(sender.text) == 2 {

}

Upvotes: 1

Related Questions