# Chapter 4 Protocol Methods

**URL:** <https://forums.kodeco.com/t/chapter-4-protocol-methods/61961>\
**Category:** Design Patterns by Tutorials\
**Created:** [February 12, 2019, 12:12am UTC](https://forums.kodeco.com/t/chapter-4-protocol-methods/61961 "2019-02-12T00:12:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![waune](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/waune/32/110937_2.png) [@waune](https://forums.kodeco.com/u/waune)\
**Post date:** [February 12, 2019, 12:12am UTC](https://forums.kodeco.com/t/chapter-4-protocol-methods/61961/1 "2019-02-12T00:12:33Z")

</div>

Curious to know what questionIndex is useful for in  
**public**  **func** questionViewController( **\_** viewController: QuestionViewController, didCancel questionGroup: QuestionGroup, at questionIndex: Int)

We’re just popping back to SelectQuestionGroupViewController each time, so it’s difficult to understand why this questionIndex is being used. Same thing goes for questionGroup, it doesn’t seem like anything other than the QuestionViewController is useful, can someone fully explain what these methods are doing?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [February 12, 2019, 11:07am UTC](https://forums.kodeco.com/t/chapter-4-protocol-methods/61961/2 "2019-02-12T11:07:38Z")

</div>

@jrg.developer Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![jrg.developer](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jrg.developer/32/109958_2.png) [@jrg.developer](https://forums.kodeco.com/u/jrg.developer)\
**Post date:** [February 12, 2019, 4:25pm UTC](https://forums.kodeco.com/t/chapter-4-protocol-methods/61961/3 "2019-02-12T16:25:16Z")

</div>

Thanks for your question, @waune.

For clarity, here’s the full question, including the protocol declaration:

> [@waune](#):
>
> Curious to know what questionIndex is useful for in
> 
> ```swift
> public protocol QuestionViewControllerDelegate: class {
>   
> func questionViewController(_ viewController: QuestionViewController,
> didCancel questionGroup: QuestionGroup,
> at questionIndex: Int)
> // ...
> }
> 
> ```
> 
> Same thing goes for `questionGroup`.

As this method is defined in `QuestionViewControllerDelegate`, it’s a **delegate callback** method. In such, it’s common for iOS delegate methods to provide contextual information via the method inputs.

For example, here’s a method signature from `UITableViewDelegate`:

```swift
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

```

The `indexPath` technically isn’t required here; the delegate _could_ get directly from the `tableView`. For example,

```swift
public func tableView(_ tableView: UITableView,
                        didSelectRowAt indexPath: IndexPath) {
    let selectedIndexPath = tableView.indexPathForSelectedRow
    indexPath == selectedIndexPath // true
}

```

Why bother with providing the `indexPath` in this method, then? Two reasons:

1. `tableView.indexPathForSelectedRow` is _optional_, but here, `indexPath` is non-optional. So, this avoids the consumer having to a forced cast/`if let`/etc to get the selected index path.

2. The consumer likely _will_ want to use `indexPath` within this delegate method.

Likewise, the reasons for the custom delegate method’s inputs are similar here:

1. It avoids the consumer having to unwrap `questionGroup`, which is technically optional as it’s declared as `QuestionGroup!`.

2. The consumer _may_ want to use `questionGroup` and `questionIndex`.

The second point here is debatable: as you’ve noted as well, the current consumer doesn’t actually care about `questionGroup` or `questionIndex`! Rather, it simply pops the view controller in response to this delegate call. Thereby, you _indeed could_ remove these parameters from the method signature, in terms of what the current consumers cares about… !

Ultimately, “computer science” is often times more of an “art” than a “science.” In cases like these, you may have to _guess_ what consumers want.

Here, this delegate method errs on the side of providing _too much_ contextual information, rather than too little. However, feel free to change your own implementation! Ultimately, you’re a developer too, which empowers you to change programming details as you’d prefer.

I hope this explanation helps you understand the motivations for writing the method this way. :] Let me know if this still isn’t clear or if you have any follow up questions about.

---

<div class="post-metadata">

**Author:** ![waune](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/waune/32/110937_2.png) [@waune](https://forums.kodeco.com/u/waune)\
**Post date:** [February 13, 2019, 11:44pm UTC](https://forums.kodeco.com/t/chapter-4-protocol-methods/61961/4 "2019-02-13T23:44:29Z")

</div>

This was perfect! Great use of comparison with the table view example. Thank You Joshua I appreciate it!
