Cannot convert value of type 'ViewController.Type' to expected argument type 'UIViewController'

Everyone,


I have done a lot of mundane trouble shooting steps with no success.


The post title indicates an error in this line of code:

window?.rootViewController = UINavigationController(rootViewController: ViewController)

from AppDelegate.swift.


Its like some part of the default UIViewController class is missing.


I also had some bugs / errors when I closed that project and opened existing projects, tried to run those and the builds failed.


I am unable to reproduce the later after software updates / uninstalls -> reinstalls. But Cannot convert value of type 'ViewController.Type' to expected argument type 'UIViewController' is a real drag.


The build errors suggested that I submit a bug report to Apple, which I did. But I was unable to attached the suggested files from: /var/folders/lz/fh9fbp5j35x_fqmk59vlfkkw0000gn


sudo cp failed (was taking forever and partially failed). But that directory contains:

0, C, T, Cleanup At Startup


Any help would be much appreciated.


-Andrew

Replies

Per convention, class types start with an uppercase letter, and variables start with a lowercase letter. In your example, you are passing "ViewController", which appears to be a class type. However, UINavigationController(rootViewController:) takes a variable of the type UIViewController. So, you should pass a variable, like this:


// Create a variable called viewController of type ViewController.
let viewController = ViewController()
window?.rootViewController = UINavigationController(rootViewController: viewController)

Shouldn't it just be whatever the class is defined to be? Convention wouldn't cause a crash:



class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


So I pass ViewController.

That is how Xcode defines the class on startup, not me...


In an unmodified project, just the stubs and stuff:



func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }


It starts with the error:

Cannot convert value of type 'ViewController.Type' to expected argument type 'UIViewController'

Forget about that convention thing, that's another issue.


You need to pass an instance of your `ViewController` for `UINavigationController(rootViewController:)`, but you are passing just a class name.

A class name cannot be an instance. Create an instance of your ViewController and pass it to `UINavigationController(rootViewController:)`.

Well thank you. That fixed it in the current project that I tried it on. But I still don't understand and still think there is a bug.


I have unmodified projects that start with errors: see my send reply to your post.

LOL, I never instantiated it. Thanks, you said the same thing as someone else but I understood this better.


Something weird is going on, though. But I will just move on.


Perhaps it was just coincidental to the other errors.