Is it possible to switch on a generic type in Swift?
Here's an example of what I mean:
func doSomething<T>(type: T.Type) {
switch type {
case String.Type:
// Do something
break;
case Int.Type:
// Do something
break;
default:
// Do something
break;
}
}
When trying to use the code above, I get the following errors:
Binary operator '~=' cannot be applied to operands of type 'String.Type.Type' and 'T.Type'
Binary operator '~=' cannot be applied to operands of type 'Int.Type.Type' and 'T.Type'
Is there a way to switch on a type, or to achieve something similar? (calling a method with a generic and performing different actions depending on the type of the generic)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…