【发布时间】:2016-04-27 04:06:49
【问题描述】:
对于像if let 或guard 这样的简单情况,我看不到优势,
if case let x? = someOptional where ... {
...
}
//I don't see the advantage over the original if let
if let x = someOptional where ... {
...
}
对于for-case-let 案例,为了简化可选集合的使用,我真的希望 swift 可以更进一步:
for case let x? in optionalArray {
...
}
//Wouldn't it be better if we could just write
for let x? in optionalArray {
...
}
在google了一段时间后,我发现唯一有用的是这个“Swift 2 Pattern Matching: Unwrapping Multiple Optionals”:
switch (username, password) {
case let (username?, password?):
print("Success!")
case let (username?, nil):
print("Password is missing")
...
那么引入可选模式还有其他好处吗?
【问题讨论】:
-
在我的观察中,如果你想在没有完整的 switch-case 的情况下检查枚举的值,这个 'if case' 或 'while case' 或 'for case' 是很方便的构造。
标签: swift swift2 switch-statement pattern-matching optional