如何在Swift中调用动态类型的泛型函数
发布时间:2020-12-14 04:56:02  所属栏目:百科  来源:网络整理 
            导读:说我有一个协议Fooable: protocol Fooable {} 现在我需要在泛型函数中使用Fooable类型: func fooingAroundFooableType: Fooable(withType: FooableType.Type) {} 当我用Fooable类型调用函数时,这很好用: struct Foo: Fooable {}fooingAround(Foo.self) //
                
                
                
            | 
 说我有一个协议Fooable: 
  
  
  protocol Fooable {}现在我需要在泛型函数中使用Fooable类型: func fooingAround<FooableType: Fooable>(withType: FooableType.Type) {}当我用Fooable类型调用函数时,这很好用: struct Foo: Fooable {}
fooingAround(Foo.self) // works fine但是,我需要检索从其他地方移交给函数的Fooable类型.这是编译器失败的地方: let fooableType: Fooable.Type = // obtain from somewhere else fooingAround(fooableType) // compiler error: "Cannot invoke 'fooingAround' with an argument list of type '(Fooable.Type)'" 具体来说,我从描述API端点的枚举中获取Fooable.Type,其中每个端点由不同的Fooable类表示. 我想问题出现是因为我动态获取了一个类型,所以在编译时不能强类型. 有办法解决这个问题吗? 解决方法
 问题是这个: 
  
  
  let fooableType: Fooable.Type = // obtain from somewhere else …正在遗忘您想要存储在该变量中的信息,即符合Fooable的具体类型是什么.请考虑以下代码编译: protocol Fooable {}
func fooingAround<FooableType: Fooable>(withType: FooableType.Type) {}
struct Foo: Fooable {}
fooingAround(Foo) // works fine
let foo = Foo()
let fooableType /* do not cast here */ = foo.dynamicType
fooingAround(fooableType) // also works fine…这意味着您必须找到一种方法直接将类型信息传递到函数调用中而不进行强制转换. 根据您考虑的fooingAround的类型,您可以例如能够沿着以下行扩展Fooable: extension Fooable {
    func fooingAround() {
        /* do some fooing with */ self.dynamicType // which is the Foo.Type when called on the `foo` value
    }
}
foo.fooingAround()(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
