相关疑难解决方法(0)

打开通用类型?

是否可以在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

尝试使用上面的代码时,我收到以下错误:

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'
Run Code Online (Sandbox Code Playgroud)

有没有办法打开一个类型,或实现类似的东西?(根据泛型的类型调用具有泛型的方法并执行不同的操作)

generics swift

20
推荐指数
1
解决办法
4869
查看次数

标签 统计

generics ×1

swift ×1