开关中的多种类型案例
Multiple type case in switch
我知道可以:
enum MyEnum {
case label
case view
case textField
}
let type = TypeEnum.label
switch type {
case .label, .textField: break //I want to this but with type.
case .view: break
}
我想知道如何使用开关进行条件测试类型。
类似于:
switch view {
case is UILabel, is UITextField: break
case is UIView: break
}
已编辑:
我知道我做错了什么。
switch view {
case is UILabel,
is UITextField,
is UILabel: break //It was telling me that this case would
//never be executed. In my tests I forgot
//to remove the duplicate.
case is UIView: break
}
无论如何谢谢 Rob,我不知道 fallthrough
您可以使用 fallthrough
:
switch view {
case is UILabel: fallthrough
case is UITextField: print("is label or text field")
default: print("is something else")
}
我知道可以:
enum MyEnum {
case label
case view
case textField
}
let type = TypeEnum.label
switch type {
case .label, .textField: break //I want to this but with type.
case .view: break
}
我想知道如何使用开关进行条件测试类型。
类似于:
switch view {
case is UILabel, is UITextField: break
case is UIView: break
}
已编辑:
我知道我做错了什么。
switch view {
case is UILabel,
is UITextField,
is UILabel: break //It was telling me that this case would
//never be executed. In my tests I forgot
//to remove the duplicate.
case is UIView: break
}
无论如何谢谢 Rob,我不知道 fallthrough
您可以使用 fallthrough
:
switch view {
case is UILabel: fallthrough
case is UITextField: print("is label or text field")
default: print("is something else")
}