在浏览文档时,我遇到了以下代码部分.
guard let button = sender as? UIBarButtonItem, button === saveButton else
{
os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
return
}
Run Code Online (Sandbox Code Playgroud)
我对Swift比较了解,我对其含义感到困惑...UIBarButtonItem, button....逗号,在这做什么?它是否符合逻辑&&?这种语法对于swift中的所有控制结构(例如if语句)是否合法,还是仅用于保护语句?
,几乎是一样的&&.
if 1 == 1, 2 == 2 {
print("dd")
}
if 1 == 1 && 2 == 2 {
print("dd")
}
Run Code Online (Sandbox Code Playgroud)
以上两个if陈述都将打印出来dd.
,可用于任何&&可以使用,如while,if和guard.
然而,if let或者guard let,如左侧没有返回Bool,&&不能使用,,必须使用.
error: test.playground:4:12: error: optional type 'String?' cannot be used as a boolean; test for '!= nil' instead
if let a = a && 2 == 2 {
^
( != nil)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
321 次 |
| 最近记录: |