【发布时间】:2015-06-26 13:27:58
【问题描述】:
我正在尝试使用泛型动态创建基于类实例的类型,但是我遇到了一些奇怪的行为。在示例 1 中,一切正常,但在示例 2 中,如果我将 Test.self 传递给泛型函数,则它不起作用。类型是一样的,一切都是一样的,我不明白为什么。
class Test{
required init(x: Int){
// Do Something
}
}
class Builder{
init(){
}
func use<T>(test2: T.Type) -> Void{
test2(x: 10) // Error: T cannot be constructed because it has no accessible initializers
}
}
// Example 1:
let test1 = Test.self
test1(x: 10)
// Example 2:
let builder = Builder()
builder.use(Test.self)
【问题讨论】:
标签: swift introspection