【发布时间】:2015-10-18 21:52:18
【问题描述】:
是否可以在 Swift 中开启泛型类型?
这是我的意思的一个例子:
func doSomething<T>(type: T.Type) {
switch type {
case String.Type:
// Do something
break;
case Int.Type:
// Do something
break;
default:
// Do something
break;
}
}
尝试使用上面的代码时,我收到以下错误:
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'
有没有办法打开一个类型,或者实现类似的东西? (使用泛型调用方法并根据泛型的类型执行不同的操作)
【问题讨论】: