Swift 2中引入的可选模式的优点/用例是什么? [英] What are the advantages/use cases of optional patterns introduced in swift 2?
问题描述
对于if let
或guard
这样的简单情况,我看不到优势,
For the simple cases like if let
or guard
I don't see the advantage,
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 the for-case-let
case to simplify working with optional collections, I really hope swift can go one step further:
for case let x? in optionalArray {
...
}
//Wouldn't it be better if we could just write
for let x? in optionalArray {
...
}
在Google上搜索了一段时间后,我发现唯一有用的情况是"快速2模式匹配:展开多个可选选项":
After google it for a while the only case I find useful is this "Swift 2 Pattern Matching: Unwrapping Multiple Optionals" :
switch (username, password) {
case let (username?, password?):
print("Success!")
case let (username?, nil):
print("Password is missing")
...
那么引入可选模式还有其他优势吗?
So any other advantages of introducing optional patterns?
推荐答案
我相信您正在混淆两个不同的概念.诚然,语法不是立即直观的,但是我希望在下面阐明它们的用法. (我建议您阅读有关 The Swift编程语言 .)
I believe you're conflating two different concepts. Admittedly, the syntax isn't immediately intuitive, but I hope their uses are clarified below. (I recommend reading the page about Patterns in The Swift Programming Language.)
条件条件"是指编写能力:
The "case condition" refers to the ability to write:
-
if case «pattern» = «expr» { ... }
-
while case «pattern» = «expr» { ... }
-
for case «pattern» in «expr» { ... }
if case «pattern» = «expr» { ... }
while case «pattern» = «expr» { ... }
for case «pattern» in «expr» { ... }
这些功能特别有用,因为它们使您无需使用switch
即可提取枚举值.
These are particularly useful because they let you extract enum values without using switch
.
您的示例if case let x? = someOptional ...
是一个有效的示例,但是我认为它对枚举除Optional 之外最有用.
Your example, if case let x? = someOptional ...
, is a valid example of this, however I believe it's most useful for enums besides Optional.
enum MyEnum {
case A
case B(Int)
case C(String)
}
func extractStringsFrom(values: [MyEnum]) -> String {
var result = ""
// Without case conditions, we have to use a switch
for value in values {
switch value {
case let .C(str):
result += str
default:
break
}
}
// With a case condition, it's much simpler:
for case let .C(str) in values {
result += str
}
return result
}
实际上,您可以将案例条件与通常在switch
中使用的几乎所有模式一起使用.有时可能很奇怪:
You can actually use case conditions with pretty much any pattern that you might normally use in a switch
. It can be weird sometimes:
-
if case let str as String = value { ... }
(等同于if let str = value as? String
) -
if case is String = value { ... }
(等同于if value is String
) -
if case 1...3 = value { ... }
(等同于if (1...3).contains(value)
或if 1...3 ~= value
)
if case let str as String = value { ... }
(equivalent toif let str = value as? String
)if case is String = value { ... }
(equivalent toif value is String
)if case 1...3 = value { ... }
(equivalent toif (1...3).contains(value)
orif 1...3 ~= value
)
另一方面,可选模式是一种模式,除了简单的if let
之外,还允许您在上下文中解开可选选项.在switch
中使用时特别有用(类似于您的用户名/密码示例):
The optional pattern, on the other hand, is a pattern that lets you unwrap optionals in contexts besides a simple if let
. It's particularly useful when used in a switch
(similar to your username/password example):
func doSomething(value: Int?) {
switch value {
//case 2: // Not allowed
case 2?:
print("found two")
case nil:
print("found nil")
case let x:
print("found a different number: \(x)")
}
}
这篇关于Swift 2中引入的可选模式的优点/用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!