Generic parameter 'T' could not be inferred


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.


The type cannot be inferred when calling a generic function returning a generic type without specifying the type of the variable you are assigning to or casting the call to the function. You can do:

let viewController: SomeViewController = self.dequeueReusableViewController()


let viewController = self.dequeueReusableViewController() as SomeViewController

I would recommend the first option unless the second is required (needing to assign to an optional for example).


There is no way the compiler know what type T is as you do not infer it.

You could force the method to know about type T:

func dequeueReusableViewController<T: UIViewController where T: Reusable>(type: T.Type) -> T? // ... let viewController = self.dequeueReusableViewController(YourViewController)

Alternatively, and somewhat neater you could let the variable do the work:

func dequeueReusableViewController<T: UIViewController where T: Reusable>() -> T? // ... let viewController: YourViewController = self.dequeueReusableViewController()

Either way you need to provide some help to let the compiler know what you're dealing with.


Should work if you are using like below example

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)


let viewController = self.dequeueReusableViewController()

is just storing the value in viewController but the type of viewController is unknown thats why it is showing you Generic parameter 'T' could not be inferred

Try let viewController: UIViewController = self.dequeueReusableViewController()

Then T will get inferred from the type UIViewController.



  • How to programmatically create a PPTP VPN connection on macOS Sierra/High Sierra?
  • 'DataFrame' object has no attribute 'get_value' in Pandas
  • Quadratic-time vertex cover verification
  • Adding extended Profile model into custom user models admin
  • How to get info on a process's virtual memory mappings in C (MacOS)
  • SUM(ABOVE) functionality in apache poi or docx4j
  • Prolog lists difference
  • Android Studio Webview not calling photo gallery
  • Simulate User Input To Call Script Multiple Times With Different Parameters
  • eclipse search in history
  • Google Places API not returning suggestions based on location
  • Accessing static method through an object instance
  • FrameLayout in reverse
  • What is the C# equivalent of BitSet of Java?
  • Unclear function return type issue
  • Django: 400 Error with Debug=False and ALLOWED_HOSTS=[“*”]
  • MSVC C4100: 'application' : unreferenced formal parameter warning
  • Syntax highlighting of Fortran 77 comments not working in vim
  • Trigger sendgrid template email using meteor
  • Second layout is not showing with include tag in android studio
  • Preparing apps for the nexus 10
  • In PHP, what is a binary string (b'xxxx')?
  • Mouse up vs. touch up in Unity
  • amcharts line to be changed with different color if it goes down to the other line(after intersectin
  • Passing multiString values to installer through command-line
  • How to use flume for uploading zip files to hdfs sink
  • Seperate list / mesh into sub-lists / sub-meshes
  • Rotate only between landscapeLeft and landscapeRight
  • Make ASP.NET web application FIPS Compliant?
  • Java Regex Finding digits in a String
  • How to use apoc.load.csv in conjunction with apoc.create.node