2016-10-12 23 views
0

這可能嗎?Swift關聯枚舉使用參數在fallthrough

 switch type { 
     case let .dog(say): fallthrough 
     case let .cat(say): 
      print(say) 
     } 

這不是有效的快速枚舉,但有沒有辦法做到這一點? 基本上我的枚舉都有相同的大小寫操作,但參數值會有所不同。

回答

2
switch type { 
case .dog(let say), .cat(let say): 
    print(say) 
} 
+0

有趣的是,這並沒有與斯威夫特2.工作 - 這是一個類似的問題http://stackoverflow.com/questions/33654074/fallthrough-in-a-switch-case-with-variable-declaration- in-swift(關閉作爲http://stackoverflow.com/questions/30545503/swift-switch-case-with-multiple-patterns-cannot-bind-to-variable)的副本,所以這可能需要一個新的答案Swift 3. –

+3

@MartinR它在Swift 3中實現:https://github.com/apple/swift-evolution/blob/master/proposals/0043-declare-variables-in-case-labels-with-multiple-patterns。 MD – Alexander