Unable to pass an array to 2 VCs

Hi, I have a cell with 2 buttons. Both buttons need to pass the same array but to 2 different view controllers depending on the user’s selection. The problem I’m having is that I cannot pass the array to one of the VCs, I get this error: “Expression type '()' is ambiguous without more context”. Any help is greatly appreciated. Here is my code:


Inside the TableViewCell

protocol PatientDataCellDelegate {
    func patCommentBtnTapped (ptData: PTData)
    func dischargeBtnTapped (ptData: PTData)
}

@IBAction func addCommentBtnTapped(_ sender: UIButton) {   //Button to segue to the Comments VC
        delegate?.patCommentBtnTapped(ptData: ptData)
    }
    
@IBAction func patientDischargeBtnTapped(_ sender: UIButton) { //Button to segue to the Discharge VC
        delegate?.dischargeBtnTapped(ptData: ptData)
    }



View Controller


      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToComments" {
            if let commtsVC = segue.destination as? CommentsVC {
                if let ptDatas = sender as? PTData {
                    commtsVC.ptDatas = ptDatas
                }
            }
        } else if segue.identifier == "goToDischarge" {
                  if let discharVC = segue.destination as? DischargeVC {
                    if let ptDataFromCell = sender as? PTData {
                        discharVC.patDischargeFromCell = ptDataFromCell
                        }
                    }
                }
            }

        }



extension PatdataVC: PatientDataCellDelegate {

     func patCommentBtnTapped (ptData: PTData) {   //Segue to Comments VC
         performSegue(withIdentifier: "goToComments", sender: ptData)
    }
 
    func dischargeBtnTapped(ptData: PTData) {.  // Segue to Discharge VC
        performSegue(withIdentifier: "goToDischarge", sender: ptData)
    }
}


DischargeVC


var patDischargeFromCell = [DischargePatient]()

Accepted Reply

I just want to pass PTData to CommentsVC (which works prefectly) and to DischargeVC

Then you need to define a property receiving `PTData` in your DischargeVC`.

Currently, you have a property receiving `[DischargePatient]`, but no property for `PTData`.


Anyway, you should better show enough code if you really want to solve your issue here.

Replies

How do you activate the segue: with performSegue in the button ?


How could sender be PTData on lines 04 and 12 ? It is an object, such as a button or the VC itself.

How are segues defined ? From Buttons ? From the VC (with the code you show that would be more logic)

How is PTData defined ?

In prepare, you have the test line 07 inside the test line 02. It can never be true.


With all this, your code should more likely look like this:


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToComments" {
        if let commtsVC = segue.destination as? CommentsVC {
            if let ptDatas = sender as? PTData {
                commtsVC.ptDatas = ptDatas
            }
        }
    }
   
    if segue.identifier == "goToDischarge" {
        if let discharVC = segue.destination as? DischargeVC {
            if let ptDataFromCell = sender as? PTData {
                discharVC.patDischargeFromCell = ptDataFromCell. //I get the error here: Expression
                //type '()' is ambiguous without more context
            }
        }
    }
}


But lines 04 and 12 remain uncorrect.

Why do you do those tests ? The sender cannot be PTData (unless PTData is the VC class itself, which would be a very bad choice of name), or they maybe a property of the sender (if it is the VC, but not if it is the button)

So maybe line 04 to 06 should be

            let ptDatas = sender.propertyForPTData // If you segue from VC, sender is the VC, propertyForPTData the var that contains the PTData you want to pass.
                commtsVC.ptDatas = ptDatas
            //  }

So there are several errors here, but you should show more code and explain the context.


There is a typo line 10. You have a dot after ptDataFromCell

Why ? Maybe just the editor adding dots after double space)


Another comment: it is not a very good practice to have a sender of type Any in the IBAction for a button ; better to explicit ender: UIButton.

You say you declare `patDischargeFromCell` is of type `[DischargePatient]`, but you declare `ptDataFromCell` as `PTData`.


What is `PTData` ?

Why are you trying to pass a `PTData` to a property of type `[DischargePatient]` ?


---

I've got an idea, you may hava a property of type `[DischargePatient]` in the class `PTData`.

class PTData {
    var patDischarge = [DischargePatient]()
    
    //...
}

And you may want to pass it to `patDischargeFromCell` of `DischargeVC`.


Then your `prepare(for:sender:)` would become like this:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToComments" {
            if let commtsVC = segue.destination as? CommentsVC {
                if let ptDatas = sender as? PTData {
                    commtsVC.ptDatas = ptDatas
                }
            }
        } else if segue.identifier == "goToDischarge" {
            if let discharVC = segue.destination as? DischargeVC {
                if let ptDataFromCell = sender as? PTData {
                    discharVC.patDischargeFromCell = ptDataFromCell.patDischarge
                }
            }
        }
    }

(Replace the property name as in your actual code.)


Anyway, the error message is sort of broken, but you need to put the right property name after dot.

I updated the code with the brackets i was missing. Now, in line 5 and 13 I'm trying to send the same array (PTData) to CommentsVC (which i have no problem sending there) and now that i added another button to send the same array but to DischargeVC, it does not work.


PTData is not the VC class.


I'm not sure which tests are you referring with this question: Why do you do those tests ?


Yes, there was a typo in line 10. I've removed the dot.

PTData it's a class that holds the array of items inside the TableViewCell. DischargePatient it's the class i'm going to use with DischargeVC.


I just want to pass PTData to CommentsVC (which works prefectly) and to DischargeVC, which is not working. Hopefully this makes sense. thank you

I just want to pass PTData to CommentsVC (which works prefectly) and to DischargeVC

Then you need to define a property receiving `PTData` in your DischargeVC`.

Currently, you have a property receiving `[DischargePatient]`, but no property for `PTData`.


Anyway, you should better show enough code if you really want to solve your issue here.

I'm not sure which tests are you referring with this question: Why do you do those tests ?


I meant why doing

if let ptDatas = sender as? PTData


Depending what sender is, if sender is a button, this will always fail