Home > Net >  Object is nil segue in swift
Object is nil segue in swift

Time:09-28

I'm trying to pass an object from one view controller to another, but the receiver receives nil and throws an error saying: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "sPropSegue" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let prop = modelProperty.props[indexPath.row]
                
                let dVeiwController = segue.destination as? PDViewController
                
                detailVeiwController?.property = prop
                
                
            }
        }
    }

code in the UIViewController

var property: Property!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        propImage.image = UIImage(named: property.imageName)
        priceLable.text = property.price
        addressTextView.text = property.address        //propImage.image = UIImage()
        // Do any additional setup after loading the view.
    }

please help me solving this problem.

Thank you in advance!

CodePudding user response:

You need to make sure segue is established from the Source vc ( not the table cell ) to the destination vc for override func prepare(for segue to be called

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   performSegue(withIdentifier:"sPropSegue",sender:nil)
}

CodePudding user response:

According to my understanding you are not getting property object in your PDViewController.

So try to debug it. Make sure that prop is not nil.

Try executing the same code after commenting viewDidLoad() code.

Also make sure you have self.table.delegate = self in the controller where you are trying to perform segue.

  • Related