Ios 通用参数';T';无法推断
我正在重构代码并添加对Swift泛型的支持。我遇到了一个编译器错误。我的代码是:Ios 通用参数';T';无法推断,ios,swift,generics,Ios,Swift,Generics,我正在重构代码并添加对Swift泛型的支持。我遇到了一个编译器错误。我的代码是: func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T { // Try to fetch view controller from the reuse queue. if !self.viewControllerReuseQueue.isEmpty {
func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T {
// Try to fetch view controller from the reuse queue.
if !self.viewControllerReuseQueue.isEmpty {
return self.viewControllerReuseQueue.popFirst()! as! T
}
// Ask delegate to instantiate a new view controller.
return delegate!.reusableViewControllerForPageViewController(self)
}
我得到一个错误:
无法推断泛型参数“T”
我怎样才能解决这个问题?我在SO上检查了类似的问题,但没有一个能描述我的案例。如果您使用下面的示例,应该可以
protocol Reusable {
func someMethod()
}
class VC: UIViewController, Reusable {
func someMethod() {
//Implement
}
}
class Dequeuer {
var viewControllerReuseQueue = [VC(),VC(),VC()]
func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T? {
// Try to fetch view controller from the reuse queue.
return viewControllerReuseQueue.first as? T
}
}
let vc: VC? = Dequeuer().dequeueReusableViewController()
print(vc)
正在将值存储在viewController中,但viewController的类型未知,这就是它显示无法推断通用参数“T”的原因
请尝试让viewController:UIViewController=self.dequeueReusableViewController()
然后将从UIViewController类型推断出T。调用返回泛型类型的泛型函数时,如果不指定要分配给该函数的变量类型或强制调用该函数,则无法推断该类型。你可以做:
let viewController: SomeViewController = self.dequeueReusableViewController()
或
我建议使用第一个选项,除非第二个选项是必需的(例如,需要指定一个可选选项)。编译器无法知道T是什么类型,因为您无法推断它 您可以强制方法了解类型T:
func dequeueReusableViewController<T: UIViewController where T: Reusable>(type: T.Type) -> T?
// ...
let viewController = self.dequeueReusableViewController(YourViewController)
func-dequeueReusableViewController(类型:T.type)->T?
// ...
让viewController=self.dequeueReusableViewController(YourViewController)
或者,您可以让变量完成以下工作:
func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T?
// ...
let viewController: YourViewController = self.dequeueReusableViewController()
func dequeueReusableViewController()->T?
// ...
让viewController:YourViewController=self.dequeueReusableViewController()
无论哪种方式,您都需要提供一些帮助,让编译器知道您在处理什么。谢谢您的回答!我尝试了
让viewController:UIViewController=self.dequeueReusableViewController()!我尝试了让viewController:UIViewController=self.dequeueReusableViewController()
,但我得到了完全相同的错误。确保UIViewController也符合Reusabled。添加了一个快速示例“让viewController=self.dequeueReusableViewController()作为!UIViewController”?同样,我得到了同样的错误并强制转换为泛型类型?返回委托!。可重用的ViewControllerFormageViewController(自)作为!没有区别。当我添加这个强制转换时,我得到了一个编译器警告:将'T'强制转换为同一类型没有任何效果
我们需要解决这个问题。一定还有别的事。将类型let x:type=genericCall()
或let x=genericCall()设置为type
应该已经解决了显示的问题。我看到您更新为在类型中包含UIViewController
,您是否向UIViewController
添加了符合可重用的扩展名?
let viewController = self.dequeueReusableViewController() as SomeViewController
func dequeueReusableViewController<T: UIViewController where T: Reusable>(type: T.Type) -> T?
// ...
let viewController = self.dequeueReusableViewController(YourViewController)
func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T?
// ...
let viewController: YourViewController = self.dequeueReusableViewController()