Nicholas Tiwari
Nicholas Tiwari

Reputation: 11

What does "Protocol Requires Function with Type" mean?

I'm using a delegate to pass a value I have stored within a function. Whenever I try to implement the delegate into my another class, I get the error "AnswerViewController" does not conform to protocol "TagToIndex Delegate". Expanded, the error yields:

Protocol requires function 'finishPassing(dictionary:)' with type '(Dictionary)->()' do you want to add a stub?

Here is the protocol:

protocol TagToIndexDelegate {
func finishPassing (dictionary:Dictionary<Int,Int>)
}

Here is the function I am trying to send the variable from:

extension MyCell: YSSegmentedControlDelegate {

func segmentedControl(_ segmentedControl: YSSegmentedControl, willPressItemAt index: Int) {
    tagToIndex[actionButton.tag] = index

    delegate?.finishPassing(dictionary: tagToIndex)
}

func segmentedControl(_ segmentedControl: YSSegmentedControl, didPressItemAt index: Int) {

}}

Where delegate is of type TagToIndexDelegate, and the variable tagToIndex which exists within willPressItemAt is the data I am passing.

And finally, the class I am trying to implement TagToIndexDelegate

class AnswerViewController: UIViewController, TagToIndexDelegate {
override func viewDidLoad() {
    super.viewDidLoad()

}
}

I feel like I've made some kind of fundamental error, but I'm not familiar enough with Swift to know what the error is.

Thanks, Nick

Upvotes: 1

Views: 884

Answers (1)

Dima
Dima

Reputation: 23634

You have defined protocol TagToIndexDelegate which requires the method finishPassing (dictionary:Dictionary<Int,Int>)implemented. Then, you say that your AnswerViewController class conforms to TagToIndexDelegate, but you never actually implement the required method. That class needs to implement the required method in order to satisfy conformance.

You can add a stub as the error suggests which would be:

func finishPassing (dictionary:Dictionary<Int,Int>)
{
  // logic here
}

You can also change the protocol function declaration to optional by adding optional in front of it like this:

optional func finishPassing (dictionary:Dictionary<Int,Int>)

As for what is the right thing to do, you'll have to decide that based on what's actually supposed to be happening in your app.

Upvotes: 1

Related Questions