carlosx2
carlosx2

Reputation: 1835

swift segue undeclared type breaks the code

I have been searching every where for a solution but i can't find it and when i look at my code and others it looks as it should be looking.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "helpUpdateProfile" ){
        var detailVC = segue.destinationViewController as DetailVC;
        detailVC.toPass = "help"

    }
}

It seems to be just on the line that says

var detailVC = segue.destinationViewController as DetailVC;

DetailVC is getting the error, i have no idea what type to declare this, also i have not find any one doing that in this code. I would really like to know what type to declare it.

i am using ios7.1 and 8+ for this app with xcode 6.x latest stable version

Upvotes: 1

Views: 1151

Answers (2)

Mr Beardsley
Mr Beardsley

Reputation: 3863

You will either need to force the cast with:

var detailVC = segue.destinationViewController as! DetailVC

Notice the ! after the as.

Or you can do:

if let detailVC = segue.destinationViewController as? DetailVC {
    detailVC.toPass = "help"
}

Notice the ? after the as.

segue.destinationViewController returns a UIViewController. What you are doing with the "as" is downcasting the UIViewController to your subclass of UIViewController. The Swift compiler is not able to determine if a downcast between a class and one of its subclasses will succeed. If you know with absolute certainty the type destinationViewController, you can force the downcast with as!. However, if the downcast fails at runtime, your application will crash. The safer option is to use optional binding and if let with as?. For more information you can read about as on the Apple Swift Blog

Upvotes: 1

SwiftStudier
SwiftStudier

Reputation: 2324

First you should post the error message, second be sure that DetailVC is selected in class of your viewController

Upvotes: 3

Related Questions