Swift - 一个if语句中的两个变量

时间:2021-09-12 14:21:24

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.

我只是在学习swift的几天之后,我做了一些研究,但要么不理解解释,要么找不到我想要的东西。

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

如果我在if语句中有两个变量,我怎么知道这两个变量是否为nil值。

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.

我看到某处我可以使用|| (2 x管道),但它对我不起作用。

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

3 个解决方案

#1


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?).

...但前者远比后者好,因为你几乎总是想要使用未包装的值(请参阅何时将可选值与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:

如果你有两个可选变量,并且你想要其中一个,取决于哪一个是nil,你可以使用nil-coalescing运算符,结合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)

(再次,类似于||的短路行为 - 如果第一部分为真,则第二部分不进行评估,因为如果它是真的无关紧要)

#2


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/

这是一些基本的东西,所以如果你不熟悉它,我肯定会建议你阅读Swift编程指南。这是链接: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.

前两个示例仅用于检查值是否为零(并且它们演示了如何使用||和&&)。如果您需要测试值是否为零然后打开它们,您应该查看@Airspeed Velocity的答案。

#3


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
}

#1


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?).

...但前者远比后者好,因为你几乎总是想要使用未包装的值(请参阅何时将可选值与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:

如果你有两个可选变量,并且你想要其中一个,取决于哪一个是nil,你可以使用nil-coalescing运算符,结合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)

(再次,类似于||的短路行为 - 如果第一部分为真,则第二部分不进行评估,因为如果它是真的无关紧要)

#2


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/

这是一些基本的东西,所以如果你不熟悉它,我肯定会建议你阅读Swift编程指南。这是链接: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.

前两个示例仅用于检查值是否为零(并且它们演示了如何使用||和&&)。如果您需要测试值是否为零然后打开它们,您应该查看@Airspeed Velocity的答案。

#3


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
}