Can't Cast Value of Type Uiviewcontroller to Patterndetailviewcontroller

Can't cast value of type UIViewController to PatternDetailViewController

The problem, as you have said, is in these lines:

if segue.identifier == "patternDetailSegue" {
var detailViewController = segue.destinationViewController as! PatternDetailViewController
// Could not cast value of type 'UIViewController' to 'Patternz.PatternDetailViewController'

The error message tells you that the destinationViewController of this segue is not, in fact, a PatternDetailViewController. You may think it is, but it isn't. You need to examine this segue in the storyboard and see what's really at the destination end of it.

The fact that the error message describes it as a UIViewController makes me suspect that you forgot to enter any view controller type in this view controller's Identity inspector in the storyboard:

Xcode Screenshot

Value of type UIViewController can never be nil, comparison isn't allowed

The declaration of the function says that fromVC is optional (that's what the ? suffix means), and that toVC is not optional (because it has no ? suffix). Only an Optional<UIViewController> can be nil.

Also, the common Swift style is to use an if-let to unwrap the optional. Try this:

private func switchViewController(from fromVC: UIViewController?, to toVC: UIViewController) {
if let fromVC = fromVC {
// In this scope, fromVC is a plain UIViewController, not an optional.
fromVC.willMoveToParentViewController(nil)
fromVC.view.removeFromSuperview()
fromVC.removeFromParentViewController()
}

self.addChildViewController(toVC)
self.view.insertSubview(toVC.view, atIndex: 0)
toVC.didMoveToParentViewController(self)
}

Can't downcast after `storyboard.instantiateViewController`

I tried the possible cases for "Could not cast value..." and it was woking fine for all cases, and then I generated this error in one case when module was not inherited from target. This solved the problem for me.

Just re-enter the class type on storyboard, it will automatically select "Inherit module from target". This would solve your problem.

Sample Image

Cannot convert a viewController object to viewController type in Swift

Think of it like this.

All DetailVCs are UIViewControllers, but not all UIViewControllers are DetailVCs.

Therefore, while you can freely convert a DetailVC to a UIViewController such as:

let detailVC = DetailVC()
let vc : UIViewController = detailVC as UIViewController

You cannot do the reverse, as the view controller might not be a DetailVC.

Therefore you can use as! in order to force the cast to the DetailVC.

let vc : UIViewController = DetailVC()
let detailVC = vc as! DetailVC

If it's a DetailVC, then it'll convert no problem; if it isn't, it will crash.

If you're not 100% sure of the type before you cast, you should always use as? in order to check the type before converting. For example:

let vc : UIViewController = DetailVC()

if let detailVC = vc as? DetailVC {
// do something
}

Or, as user965972 says, you can go one step further by using a guard statement to prevent any further execution if the casting fails. For example:

let vc : UIViewController = DetailVC()

guard let detailVC = vc as? DetailVC else {
// uh oh, casting failed. maybe do some error handling.
return
}

// freely use detailVC from the this point onwards, with the correct type.

The method you use depends entirely on if you want to handle errors and whether further execution depends on the casting being successful.

Storyboard doesn't contain a view controller with identifier 'goToC'

Xcode 8.2.1

I thought CameraViewController is in your Main storyboard.

In Main story board first select yellow button top of the appropriate viewController and goto identity inspector -> identity -> Storyboard ID enter your view controller identifier for this case goToC

Sample Image

Why am I getting a seg fault in this line of code when I try to segue from storyboard?

This is a case where the console error tells you everything you need to know. segue.destination is not a BarcodeReaderViewController so your attempt to cast it to one (with as!) is causing a catastrophic failure.

First, check in your storyboard and make sure you set the type of the view controller properly (the one that you are transitioning to.)

Do this by selecting the appropriate view controller in the storyboard and checking here:

location where class name must be entered.

Cast from 'UIViewController' to unrelated type 'TableViewCell' always fails

-[UIStoryboardSegue destinationViewController] returns a UIViewController*. Assuming TableViewCell is a subclass of UITableViewCell, the cast does not make sense. From the information I see, you'll want to check if the destinationViewController points to an instance of UITableViewController and then cast it.

UIViewController *viewController = [segue destinationViewController];
if ([viewController isKindOfClass:[UITableViewControler class]])
{
UITableViewController *tableViewController = (UITableViewController *) viewController;
}

From there, you can access the cells using any of UITableViewController's methods, probably those that implement UITableViewDelegate or UITableViewDataSource. If you require more implementation specific control you can cast to TableVC after checking that destinationViewController is an instance of that class.

How to check type against type parameter?

In your last example, change

if vc is type 

to

if vc is T

T is the type you’re looking for, whereas T.Type is a metatype giving information about the type T. All you’re really using T.Type for is to fix T to be the type that you want when you call getChildViewController.



Related Topics



Leave a reply



Submit