ios Swift 中的单行 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24158674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:48:32  来源:igfitidea点击:

Single line if statement in Swift

iosobjective-cswiftswift3swift4

提问by BytesGuy

How would one convert the following to Swift from Objective-C?

如何将以下内容从 Objective-C 转换为 Swift?

if (myVar) return;

Swift does not use parentheses around the conditional, however the following code gives an error.

Swift 不会在条件周围使用括号,但是以下代码给出了错误。

if myVar return 

回答by 4aRk Kn1gh7

Well as the other guys also explained that braces are a must in swift. But for simplicity one can always do something like:

正如其他人也解释说,大括号是 swift 必须的。但为了简单起见,人们总是可以这样做:

let a = -5

// if the condition is true then doThis() gets called else doThat() gets called
a >= 0 ? doThis(): doThat()

func doThis() {
    println("Do This")
}

func doThat() {
    println("Do That")
}

回答by Mick MacCallum

In Swift the braces aren't optional like they were in Objective-C (C). The parens on the other hand are optional. Examples:

在 Swift 中,大括号不像在 Objective-C (C) 中那样是可选的。另一方面,括号是可选的。例子:

Valid Swift:

有效的斯威夫特:

if someCondition {
    // stuff
}

if (someCondition) {
    // stuff
}

Invalid Swift:

无效的斯威夫特:

if someCondition 
    // one liner

if (someCondition)
    // one liner

This design decision eliminates an entire class of bugs that can come from improperly using an if statement without braces like the following case, where it might not always be clear that something's value will be changed conditionally, but somethingElse's value will change every time.

此设计决策消除了由于不正确使用不带大括号的 if 语句而产生的整类错误,如下例所示,在这种情况下,可能并不总是清楚something的值将有条件地更改,但somethingElse的值每次都会更改。

Bool something = true
Bool somethingElse = true

if (anUnrelatedCondition) 
    something = false
    somethingElse = false

print something // outputs true
print somethingElse // outputs false

回答by Azhar

You can use new Nil-Coalescing Operator, since Swift 3 in case when you need just set default value in case of if fails:

您可以使用新的 Nil-Coalescing 运算符,因为 Swift 3 以防万一您只需要在 if 失败的情况下设置默认值:

let someValue = someOptional ?? ""

In case if someOptional is false, this operator assign "" to someValue

如果 someOptional 是false,则此运算符将 "" 分配给 someValue

var dataDesc = (value == 7) ? "equal to 7" : "not equal to 7"

回答by Sulthan

One-line if, one-line whileand one-line forare considered a bad style by many developers because they are less readable and allegedly a source of many errors.

One-line if、 one-linewhile和 one-linefor被许多开发人员认为是一种糟糕的风格,因为它们可读性较差,并且据称是许多错误的来源。

Swift solved the conundrum by forbidding one-line flow control statements; the braces are non-optional...

Swift 通过禁止单行流控制语句解决了这个难题;大括号是非可选的......

if someCondition {
     // stuff
}

Of course, you can still do

当然,你仍然可以这样做

if someCondition { return }

There are also implementation reasons. Having the parentheses around the condition as optional makes the parsing much harder. Enforcing braces simplifies parsing again.

还有执行原因。将条件周围的括号设为可选会使解析更加困难。强制大括号再次简化了解析。

回答by Sathya Baman

Here is a simple solution I used in my projects.

这是我在项目中使用的一个简单解决方案。

Swift 4+

斯威夫特 4+

isManageCardTnCSelected ?
      (checkbox.isSelected = true) : (checkbox.isSelected = false)

var selected: Bool = isManageCardTnCSelected ?
      checkbox.isSelected = true : checkbox.isSelected = false

Swift 3+

斯威夫特 3+

var retunString = (state == "OFF") ? "securityOn" : "securityOff"