iosswiftsegueoption-typeforced-unwrapping

When trying to segue to a view controller from a table view i get this error: Unexpectedly found nil while unwrapping


I have a segue named "hydrogenSegue" from a "hydrogenBoxButton" to a "Hydrogen" view controller. However, I also wanted to implement a table view so I could search for an element. I tried to make the code so when the cell is clicked it will segue over to the element's view. I used hydrogen as an example here.

In my main ViewController.swift file, I have this to transfer the data:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    //If identifier equals the hydrogen element go to the elements Swift file

    if segue.identifier == "hydrogenSegue" {

        let hydrogenAtomicNumberPassing = segue.destination as! hydrogenViewController
        hydrogenAtomicNumberPassing.hydrogenAtomicNumberPassed = hydrogenAtomicNumber

        let hydrogenAtomicMassPassing = segue.destination as! hydrogenViewController
        hydrogenAtomicMassPassing.hydrogenAtomicMassPassed = hydrogenAtomicMass


    }
}

In the hydrogenViewController.swift file I have this:

import UIKit

class hydrogenViewController: UIViewController {

var hydrogenAtomicNumberPassed: Int!
var hydrogenAtomicMassPassed: Float!

@IBOutlet weak var hydrogenInformationLabel: UILabel!
@IBOutlet weak var hydrogenAtomicNumberLabel: UILabel!
@IBOutlet weak var hydrogenAtomicMassLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    //Setting the background color
    self.view.backgroundColor = UIColor.gray

    //Converting hydrogen's atomic number from an Int to a String
    let hydrogenAtomicNumberString = String("\(hydrogenAtomicNumberPassed!)")
    hydrogenAtomicNumberLabel.text = "Atomic Number: \(hydrogenAtomicNumberString)"

    //Converting hydrogen's atomic mass from a Float to a String
    let hydrogenAtomicMassString = String("\(hydrogenAtomicMassPassed!)")
    hydrogenAtomicMassLabel.text = "Atomic Mass: \(hydrogenAtomicMassString)"

}

}

I am getting the error at:

let hydrogenAtomicNumberString = String("\(hydrogenAtomicNumberPassed!)")

I'm assuming it would happen to this line also if I fix only that line:

let hydrogenAtomicMassString = String("\(hydrogenAtomicMassPassed!)")

I have this code in my "searchViewController" (the .swift file used for the table view):

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         print("row selected : \(indexPath.row)")

         if indexPath.row == 0 {

             let hydrogenSearchSegue = UIStoryboard(name:"Main", 
                bundle:nil).instantiateViewController(withIdentifier: "hydrogenView") as! 
                hydrogenViewController

             self.navigationController?.pushViewController(hydrogenSearchSegue, 
             animated:true)
     }

}

When I click on the "Hydrogen" cell in the table view it crashes to this error:

Hydrogen cell

The crash

When I click on the "H" button in this image it will take me to the hydrogen view controller:

Image of the Hydrogen Button in the simulator (Top Left)

Image of the Hydrogen View Controller

I want the hydrogen cell to segue over to the hydrogen view controller just like the button can.

When this same issue came up earlier I just had an issue with the name of the segue in the storyboard. However, because there is no visible segue from the table view, I don't know how to fix the issue.

I've tried this:

        performSegue(withIdentifier: "hydrogenSegue", sender: nil)

I was thinking that I could just reuse the "hydrogenSegue" from the button to the view controller but I get a SIGABRT error. It just says that there is no segue with the name "hydrogenSegue." It would be best if I could just reuse that segue in a way because everything is already connected but I now found out that the "searchViewController" can't recognize the segue. Any help is appreciated and my main goal is to just get the cell that is clicked on to move over to the element's designated view. I tried to provide as much information as possible without making it to long and if there is any more information needed, I should be able to provide it.


Solution

  • well. first answer

    in your hydrogenViewController try with this lines.

    var hydrogenAtomicNumberPassed: Int?
    var hydrogenAtomicMassPassed: Float?
    
    override func viewDidLoad(){
       super.viewDidLoad()
       self.viewBackgroundColor = .gray
    }
    
    override func viewWillAppear(){
       super.viewWillAppear()
       if let number = hydrogenAtomicNumberPassed
       {
           hydrogenAtomicNumberLabel.text = "Atomic Number: \(number)"
       }
       if let mass = hydrogenAtomicMassPassed
       {
           hydrogenAtomicMassLabel.text = "Atomic Mass: \(mass)"
       }
    }
    

    Now, the segues only "lives" between a couple viewControllers, if you have a third view controller, the last will not recognize him.

    other thing, you are using segues and navigation controller, from my point of view, it's a bad idea mixed both, I mean, there are specific apps that can use both ways to present views, only is a advice.

    if you want to pass data with pushviewcontroller only use this line

    if indexPath.row == 0 {
         let hydrogenSearchSegue = UIStoryboard(name:"Main",bundle:nil).instantiateViewController(withIdentifier: "hydrogenView") as! hydrogenViewController
         hydrogenSearchSegue.VAR_hydrogenViewController = YOURVAR_INYOURFILE
         self.navigationController?.pushViewController(hydrogenSearchSegue, animated:true)
    }
    

    tell me if you have doubts, and I will try to help you.