Umesh Sharma Umesh Sharma - 5 months ago 8
iOS Question

Cannot assign value of type 'UIViewController' to type 'LeftViewController!'. Why?

I have a class LeftViewController like this

protocol LeftMenuProtocol: class {
func actionOpenHome()
func actionOpenTakeSnap()
func actionOpenGallery()
func actionOpenProfiles()
func actionOpenSettings()
func actionOpenHelp()
func actionOpenContactUs()
func actionOpenBarCode()
func actionOpenAbout()
func actionCloseSideVC()
}

class LeftViewController : UIViewController {

@IBOutlet weak var tableView: UITableView!
var leftMenuDelegate : LeftMenuProtocol?


required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func viewDidLoad() {
super.viewDidLoad()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.view.layoutIfNeeded()
}
}`


I am trying to add it into another class like this

var leftViewController: LeftViewController!
leftViewController = self.storyboard!.instantiateViewControllerWithIdentifier("LeftViewController")
self.addChildViewController(leftViewController)
self.view!.addSubview(leftViewController.view!)
leftViewController.didMoveToParentViewController(self)`


But getting this error. Somebody knows what is wrong?

Answer

change the variable declaration to

leftViewController = self.storyboard!.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController

You need to explicitly cast it.

Comments