0

How can I pass additional parameters to a selector @objc function in Swift 5?

What I would like to do is this:

    @objc func sendLike(_ sender: Any, checkin: String) {
    
    createLikeCheckin(checkin: checkin) // This is the URLSession function
}

// In tableView cellForRowAt
let checkin = self.userCheckins[indexPath.row]
cell.likeButton.addTarget(self, action: #selector(sendLike(_:, checkin: checkin.pk)), for: .touchUpInside)

So I can pass the checkin object's pk to then use in a URLSession. However I get the error: Missing argument for parameter #1 in call Insert '<#Any#>, '

which leads me to this error after selecting fix: Argument of '#selector' does not refer to an '@objc' method, property, or initializer

Booshwa
  • 25
  • 5
  • You can't do that. Partial application of a function is not a selector. You might want to have a look at how to turn a closure into a selector, e.g. https://gist.github.com/cprovatas/98ff940140c8744c4d1f3bcce7ba4543 – Sweeper Aug 14 '21 at 05:05
  • Does this answer your question? [Cannot add target with parameters in CollectionViewCell's button (Swift 4)](https://stackoverflow.com/questions/49211252/cannot-add-target-with-parameters-in-collectionviewcells-button-swift-4) – vadian Aug 14 '21 at 07:41
  • Those are both helpful suggestions! – Booshwa Aug 14 '21 at 15:29

1 Answers1

0

It turns out I'm just horrible at searching

Answer: Pass multiple parameters to addTarget

Some other references: Attach parameter to button.addTarget action in Swift

Booshwa
  • 25
  • 5