Swift typecasting/pattern-match 调节这个或那个
Swift typecasting/pattern-match condition this or that
我的用例涉及一个 switch 语句并试图构建一个依赖于多个类型转换的案例。这将真正帮助我介绍一些 composable/clean/less 代码。通用的多重条件将不起作用,因为我将 let
与转换一起使用,并且它不能被改变。此外,fallthrough 会产生类似的错误。
case let foo as SomeStruct,
let foo as SomeOtherStruct:
// do whatever
Error: Pattern variable bound to type
case let foo as SomeStruct:
fallthrough
case let foo as SomeOtherStruct:
// do whatever
'fallthrough' cannot transfer control to a case label that declares variables
这些错误都是有道理的。基本上,我问是否有任何方法可以做 let foo as SomeStruct || SomeOtherStruct
的 typecasting/pattern-match
编辑:为了获得更多上下文,我正在使用 ReSwift 进行减速器组合
那这个呢?
struct S1 {
var a : String
}
struct S2 {
var a : Int
}
let x = S1(a: "Test")
switch x {
case let y where (y is S1) || (y is S2): print("S1 or S2")
default: break
}
我的用例涉及一个 switch 语句并试图构建一个依赖于多个类型转换的案例。这将真正帮助我介绍一些 composable/clean/less 代码。通用的多重条件将不起作用,因为我将 let
与转换一起使用,并且它不能被改变。此外,fallthrough 会产生类似的错误。
case let foo as SomeStruct,
let foo as SomeOtherStruct:
// do whatever
Error: Pattern variable bound to type
case let foo as SomeStruct:
fallthrough
case let foo as SomeOtherStruct:
// do whatever
'fallthrough' cannot transfer control to a case label that declares variables
这些错误都是有道理的。基本上,我问是否有任何方法可以做 let foo as SomeStruct || SomeOtherStruct
编辑:为了获得更多上下文,我正在使用 ReSwift 进行减速器组合
那这个呢?
struct S1 {
var a : String
}
struct S2 {
var a : Int
}
let x = S1(a: "Test")
switch x {
case let y where (y is S1) || (y is S2): print("S1 or S2")
default: break
}