use*_*482 3 iphone ios swift swift5.2 xcode11.4
我正在尝试实现两个选项值之间的切换:
import UIKit
var numOne: Int?
var numTwo: Int?
func doSomething() {
switch (numOne, numTwo) {
case (!nil, nil):
print("something")
case (_ == _):
print("equal")
default:
break
}
}
doSomething()
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
在第一种情况下,我收到此错误:
'nil' is not compatible with expected argument type 'Bool'
在第二种情况下,我收到了另一个错误:
Expression pattern of type 'Bool' cannot match values of type '(Int?, Int?)'
我对你们的问题是我怎样才能设法在这个和可选值之间生成案例?
我会非常感谢你的帮助
这里没有错;语法不正确。
对于第一种情况,您的意思是:
case (.some, .none):
Run Code Online (Sandbox Code Playgroud)
没有这样的东西!nil。Optionals 不是布尔值。你也可以写,(.some, nil)因为nil是 的别名.none,但感觉很混乱。
对于第二种情况,您的意思是:
case _ where numOne == numTwo:
Run Code Online (Sandbox Code Playgroud)
这里的重点是您的意思_是条件为真的所有情况 ( ) ( where ...)。
func doSomething() {
switch (numOne, numTwo) {
case (.some, .none):
print("something")
case _ where numOne == numTwo:
print("equal")
default:
break
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
127 次 |
| 最近记录: |