Protocol Not Working

I'm trying to transfer a tableview cell from my hockeydetailVC to my favouritesVC using a button and I can't transfer it due to some issue.

My hockeydetailVC

Code Block
protocol addThis {
  func addFav(data: CurrentPlayers)
}


I'm trying to transfer my item object
Code Block
var item: CurrentPlayers?
 
var delegate: addThis? = nil


Code Block
func sendData() {
    if delegate != nil {
      let data = item!
      delegate?.addFav(data: data)
      print("This is the data being transferred: \(data)")
    }
  }
   
  @IBAction func addToFav(_ sender: Any) {
    sendData()
    print("Favourite button Pressed")
  }


My favouritesVC

Code Block
 var currentFav: CurrentPlayers?


Code Block
func addFav(data: CurrentPlayers) {
    currentFav = data
  }

I have a segue for my button so I have this

Code Block
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "r" {
    let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
    hockeyDetailVC.delegate = self
  }
 }


If the transfer works then my sendData() method up top would print a statement but my addToFav only prints "favourite button pressed".
Answered by ZoneX in 660866022
I'm going to close this thread.

If the transfer works then my sendData() method up top would print a statement but my addToFav only prints "favourite button pressed".

Then I guess delegate is nil when sendData() is called.


By the way, please follow the simple coding rule of Swift -- type names should start with Capital letter.
Your code is hard to read.
You just post thumbnail views of your code, it is impossible to guess where the problem is.

Does your class conform to the AddThis protocol ?
Yeah my class conforms to the protocol addThis class favouritesVC: UITableViewController, addThis . I intialized my delegate to not be nil and it still does not show the print statement from sendData.

I intialized my delegate to not be nil 

Hidden parts of your code may be affecting. For example, The actual instance of the view controller running sendData() may not be the same instance you initialized.

If you could show enough code, we would not need such guess game.
The question is how you initialize the destination controller.

I intialized my delegate to not be nil 

probably not.
As explained, you may set the delegate on an instance and then create and use another one somewhere.

Add this print to confirm:
Code Block
func sendData() {
if delegate != nil {
let data = item!
delegate?.addFav(data: data)
print("This is the data being transferred: \(data)")
} else {
print("The delegate is nil")
}
}

You could also check the instance:
Code Block
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "r" {
let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
// forcing with as! is dangerous;
// you'd better use:
// if let hockeyDetailVC = segue.destination as? HockeyDetailVC {
    hockeyDetailVC.delegate = self
    print("destination HockeyDetailVC is", hockeyDetailVC)
}
}

and in HockeyDetailVC viewDidload, add:
Code Block
print("invoked HockeyDetailVC is", self)

Report all the results

Btw this is all the code related to my protocol and what I am trying to transfer. As for the data on the results

Code Block
print("invoked HockeyDetailVC is", self)


gives me invoked HockeyDetailVC is <LearnHockey.HockeyDetailVC: 0x7fe0b3026200>. Can you explain the 0x7fe0b3026200.

Code Block
print("The delegate is nil")


gives me The delegate is nil.

Code Block
print("destination HockeyDetailVC is", hockeyDetailVC)


This does not print anything to my console.

This does not print anything to my console.

That means the segue with identifier "r" is not triggered. Better check how you are showing HockeyDetailVC.
So there is a problem in prepare.

Instrument more the code:

Code Block
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("segue identifier", segue.identifier)
if segue.identifier == "r" {
let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
hockeyDetailVC.delegate = self
}
}

and tell what you get.
If you get something different from
segue identifier r
such as
segue identifier R
or
segue identifier SomeString
that means that "r" is not the identifier of this segue.
Then change "r" by what you got here after "segue identifier"

Note:
Code Block
print("invoked HockeyDetailVC is", self)

gives me invoked HockeyDetailVC is <LearnHockey.HockeyDetailVC: 0x7fe0b3026200>. Can you explain the 0x7fe0b3026200.

0x7fe0b3026200 is simply the address o self.
The point would be to compare with the instance in HockeyDetailVC viewDidload

But the problem is most likely in prepare.

Okay I can't do segue.destination as? HockeyDetailVC but is (segue.destination as? HockeyDetailVC)! okay?

Code Block
print("segue identifier", segue.identifier)


does not give me any response on the console.

does not give me any response on the console.

That means prepare(for:sender:) is not called. Are you really using segue to show HockeyDetailVC?
Aren't you using present to show it?

Okay I can't do segue.destination as? HockeyDetailVC but is (segue.destination as? HockeyDetailVC)! okay? 

Did you write

Code Block
    if let hockeyDetailVC = segue.destination as? HockeyDetailVC  {
  hockeyDetailVC.delegate = self
}

print("segue identifier", segue.identifier)
does not give me any response on the console.

Please tell exactly how you call the segue:
  • by defining in IB ? If so, from where to where exactly

  • in code with performSegue ? If so, show the complete func where it is called

This is how I define the function

Code Block
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "r" {
    print("segue identifier", segue.identifier)
    let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
    hockeyDetailVC.delegate = self
    print("destination HockeyDetailVC is", hockeyDetailVC)
  }
 }


I don't use an IB but I have an identifier I named r in the storyboard.

I have an identifier I named r in the storyboard.

That has no meaning. Even if you have a segue with identifier r, you need to properly invoke it.


By the way, print("segue identifier", segue.identifier) needs to places outside of if-statement to explore what's happening to your project.
Even if I place it outside the r it still does not print I may have to do this a different way.

Even if I place it outside the r it still does not print I may have to do this a different way.

Then it's must not may. Please share the solution if you solved it by yourself.
Protocol Not Working
 
 
Q