I'm refactoring my code and adding support for Swift generics. I'm stuck with a compiler error. My code is:
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)
}
This compiles smoothly. Then, later, when I try to dequeue a view controller:
// Get view controller from the reuse queue.
let viewController: UIViewController = self.dequeueReusableViewController()
I'm getting an error:
Generic parameter 'T' could not be inferred
How can I solve this? I checked similar questions on SO but none of them describes my case.