1

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

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
Random206
  • 757
  • 6
  • 19
  • You’re getting down voted because this seems like a basic question, but I’d say the possible answer is more complex than people might think... – Airspeed Velocity May 03 '15 at 03:30
  • Thanks @AirspeedVelocity, at least someone understands what it's like to be new to coding...this is like my 2nd day at it, and I am self teaching using tutorials and step throughs...not simple, but I will get there :) – Random206 May 03 '15 at 05:39

3 Answers3

4

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.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Thanks @ABakerSmith for altering the question for the script block, I made this question on my phone so couldn't figure out how to do it in a script block properly. I tried that syntax exactly and it didn't work for me. Still have an error when I did it. I shall look again. – Random206 May 03 '15 at 02:28
  • Yeah, have another look because that should definitely work. Let me know what the error you're having is if it persists. – ABakerSmith May 03 '15 at 02:32
  • Hi @ABakerSmith, it worked for me :)....thank you very much for your help and understanding it is very much appreciated and you just helped me learn something new. Thank you. – Random206 May 03 '15 at 05:49
  • Glad I could help :) Also, you should definitely have a look through @Airspeed Velocity's answer which goes into detail about the different ways you can unwrap multiple optional values. – ABakerSmith May 03 '15 at 08:54
3

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)

Community
  • 1
  • 1
Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
1

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
}
pfj
  • 187
  • 1
  • 7
  • Thanks @pfj this worked for me, it is similar to the ABakerSmiths answer, if I could mark up, I would, but unfortunately I don't have that rep level yet. I gave the correct answer to ABakerSmith as he was in on the question first, but this is the correct answer also, just yours has braces around the variables. Thanks for the extra info about the && sings also, that will help in the future. – Random206 May 03 '15 at 05:52