我正在重构代码并且添加对Swift泛型的支持。但是我遇到了编译器错误。我的代码如下:
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)
}
这个编译很顺利。但是,当我尝试出列一个视图控制器时出现问题:
// Get view controller from the reuse queue.
let viewController: UIViewController = self.dequeueReusableViewController()
我遇到了一个错误:
无法推断通用参数 'T'
我该如何解决这个问题?我在stackoverflow上查看了类似的问题,但没有一个能够描述我的情况。
将'T'强制转换为相同类型没有效果
。 - Rafał Sroka