Random206
Random206

Reputation: 826

Swift - Two variables in one if statement

I'm only a few days into learning swift and I've done a bit of research but either don't understand the explanations or I can't find what I'm looking for.

If I have two variable in an if statement, how can I tell if either variable is a nil value.

For example: (syntax is not correct but just giving an idea).

if variable1 or variable2 != nil { 
    // Do this 
} else {
    // Do this instead 
}

I need to know how to do the 'or' part of it.

I saw somewhere I could use || (2 x pipes) but it didn't work for me.

I would also like to know if the same would work for something other than variables such as.

if inputTextBox1.text or inputTextBox2.text != nil { 
    // Do this
} else { 
    // Do this instead
}

Thanks,

Toby

Upvotes: 1

Views: 6095

Answers (3)

Airspeed Velocity
Airspeed Velocity

Reputation: 40973

This isn’t quite as basic a question as it seems.

Regular if…let is like an &&, that is,

let i: Int? = nil
let j: Int? = 2

if let a = i, b = j {
    // won’t execute, as i is nil
}

// is equivalent to
if let i != nil && j != nil {
    // also won’t execute
}

…but the former is far preferable than the latter, as you almost always want to use the unwrapped value (see When should I compare an optional value to nil?).

If you have two optional variables, and you want one of them, depending on which one is nil, you can use the nil-coalescing operator, in conjunction with if…let:

let i: Int? = nil
let j: Int? = 2

if let eitherOr = i ?? j {
    // prints 2, because first one is nil
    println(eitherOr)
}

// which is similar to
if i != nil || j != nil {

}

Note, though, that in the case where neither is nil, you will get the first one:

let k: Int? = 3

if let eitherOr = j ?? k {
    // prints the first non-nil value, 2
    println(eitherOr)
}

(again, analogous to short-circuiting behavior of || – if the first part is true, the second is not evaluated as it doesn't matter if it's true or not)

Upvotes: 3

ABakerSmith
ABakerSmith

Reputation: 22969

This is some basic stuff so if you're unfamiliar with it I'd definitely recommend going through The Swift Programming Guide. Here's the link: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/

In response to your question though, yes, you need to use ||, like so:

if inputTextBox1.text != nil || inputTextBox2.text != nil { 
    // Do this
} else { 
    // Do this instead
}

It's the same for 'and', just replace || with &&:

if inputTextBox1.text != nil && inputTextBox2.text != nil { 
    // Do this
} 

The previous two examples a good for just checking if the values are nil or not (and they demonstate how to use || and &&). If you need to test whether the values are nil and then unwrap them you should have a look at @Airspeed Velocity's answer.

Upvotes: 4

pfj
pfj

Reputation: 187

I had a little trouble figuring out exactly what you meant, so here's two answers:

if (variable1 == nil) || (variable2 == nil) {
//then one of them is nil; do something here
}

if (variable1 != nil) && (variable2 != nil) {
//then neither of them is nil; do something here
}

Upvotes: 1

Related Questions