Generics 泛型中的Swift通配符

Generics 泛型中的Swift通配符,generics,swift,wildcard,Generics,Swift,Wildcard,我目前正在做的项目使用了很多泛型。我在尝试使用通配符时遇到问题 在Java中,我可以做到: public class testGeneric<T> { } public class testing { public <T> void test (testGeneric<T> input) { testGeneric<?> testOne = input; } } 公共类testGeneric{ } 公共类测试

我目前正在做的项目使用了很多泛型。我在尝试使用通配符时遇到问题

在Java中,我可以做到:

public class testGeneric<T> {

}
public class testing {
    public <T> void test (testGeneric<T> input) {
          testGeneric<?> testOne = input;
    }
}
公共类testGeneric{
}
公共类测试{
公共无效测试(testGeneric输入){
testGeneric testOne=输入;
}
}
没有任何错误

在Swift中,我使用的是AnyObject(因为它是所有对象的基础,所以我假设它也可以以同样的方式使用)

但是,以下抛出错误(特别是“T与任何对象都不相同”):

类testGeneric{
}
类测试{
func测试(输入:testGeneric){
var testOne:testGeneric=input
}
}
在搜索了一段时间后,我发现Swift中有“通配符模式”(特别是“”)。然而,这似乎不适用于泛型

有办法解决这个问题吗


提前谢谢。

我想你要找的是:

class testGeneric<T> {
}

class testing {
    func test <T> (input: testGeneric<T>) {
        var testOne: testGeneric<T> = input
    }
}
类testGeneric{
}
类测试{
func测试(输入:testGeneric){
var testOne:testGeneric=input
}
}

AnyObject
不包括结构和原语。您可以尝试
任何
,除了函数之外的所有内容都符合,但我不确定这是否会work@connor我试过用任何一种,不幸的是它也有同样的问题。你可能想试试演员阵容<代码>如果让var testOne:testGeneric=input as?testGeneric{/*code*/}注意,如果t是一个函数,这意味着它将不起作用type@connor不幸的是,铸造抛出同样的错误。谢谢你花时间想一想你到底想做什么?将testGeneric的任何实例传递到测试中?
class testGeneric<T> {
}

class testing {
    func test <T> (input: testGeneric<T>) {
        var testOne: testGeneric<T> = input
    }
}