Ana*_*Ana 2 switch-statement swift
我有以下代码:
class Base {}
class A: Base {}
class B: Base {}
class C: Base {}
func next(obj: Base) -> Base {
if obj is A { return B() }
else if obj is B { return C() }
else if obj is C { return A() }
else { return A() }
}
Run Code Online (Sandbox Code Playgroud)
如何将chained ifs 表示为单个switch语句?
extension Base {
func next() -> Base {
switch self {
case is A: return B()
case is B: return C()
case is C: return A()
default: return A()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1735 次 |
| 最近记录: |