在Swift模式匹配元组时如何解包Optional?

时间:2022-09-11 18:51:41

In Swift, there's a common if let pattern used to unwrap optionals:

在Swift中,有一个常见的if let模式用于解包选项:

if let value = optional {
    print("value is now unwrapped: \(value)")
}

I'm currently doing this kind of pattern matching, but with tuples in a switch case, where both params are optionals:

我目前正在进行这种模式匹配,但在切换情况下使用元组,其中两个参数都是可选项:

//url is optional here
switch (year, url) {
    case (1990...2015, let unwrappedUrl):
        print("Current year is \(year), go to: \(unwrappedUrl)")
}       

However, this prints:

但是,这打印:

"Current year is 2000, go to Optional(www.google.com)"

Is there a way I can unwrap my optional and pattern match only if it's not nil? Currently my workaround is this:

我有没有办法解开我的可选和模式匹配只有它不是零?目前我的解决方法是:

switch (year, url) {
    case (1990...2015, let unwrappedUrl) where unwrappedUrl != nil:
        print("Current year is \(year), go to: \(unwrappedUrl!)")
}       

3 个解决方案

#1


10  

You can use the x? pattern:

你可以使用x?模式:

case (1990...2015, let unwrappedUrl?):
    print("Current year is \(year), go to: \(unwrappedUrl)")

x? is just a shortcut for .some(x), so this is equivalent to

X?只是.some(x)的快捷方式,所以这相当于

case (1990...2015, let .some(unwrappedUrl)):
    print("Current year is \(year), go to: \(unwrappedUrl)")

#2


1  

with a switch case you can unwrap (when needed) and also evaluate the unwrapped values (by using where) in the same case.

使用开关盒,您可以打开(在需要时)并在同一情况下评估未包装的值(通过使用where)。

Like this:

let a: Bool? = nil
let b: Bool? = true

switch (a, b) {
case let (unwrappedA?, unwrappedB?) where unwrappedA || unwrappedB:
  print ("A and B are not nil, either A or B is true")
case let (unwrappedA?, nil) where unwrappedA:
  print ("A is True, B is nil")
case let (nil, unwrappedB?) where unwrappedB:
  print ("A is nil, B is True")
default:
  print("default case")
}

#3


0  

you can do like this:

你可以这样做:

switch(year, x) {
    case (1990...2015,.Some):
    print("Current year is \(year), go to: \(x!)")
}

and you can also do

你也可以这样做

 switch(year, x) {
    case (1990...2015, let .Some(unwrappedUrl)):
    print("Current year is \(year), go to: \(unwrappedUrl)")
}

#1


10  

You can use the x? pattern:

你可以使用x?模式:

case (1990...2015, let unwrappedUrl?):
    print("Current year is \(year), go to: \(unwrappedUrl)")

x? is just a shortcut for .some(x), so this is equivalent to

X?只是.some(x)的快捷方式,所以这相当于

case (1990...2015, let .some(unwrappedUrl)):
    print("Current year is \(year), go to: \(unwrappedUrl)")

#2


1  

with a switch case you can unwrap (when needed) and also evaluate the unwrapped values (by using where) in the same case.

使用开关盒,您可以打开(在需要时)并在同一情况下评估未包装的值(通过使用where)。

Like this:

let a: Bool? = nil
let b: Bool? = true

switch (a, b) {
case let (unwrappedA?, unwrappedB?) where unwrappedA || unwrappedB:
  print ("A and B are not nil, either A or B is true")
case let (unwrappedA?, nil) where unwrappedA:
  print ("A is True, B is nil")
case let (nil, unwrappedB?) where unwrappedB:
  print ("A is nil, B is True")
default:
  print("default case")
}

#3


0  

you can do like this:

你可以这样做:

switch(year, x) {
    case (1990...2015,.Some):
    print("Current year is \(year), go to: \(x!)")
}

and you can also do

你也可以这样做

 switch(year, x) {
    case (1990...2015, let .Some(unwrappedUrl)):
    print("Current year is \(year), go to: \(unwrappedUrl)")
}