Group Group Group Group Group Group Group Group Group

Chapter 4 Protocol Methods


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?


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


Thanks for your question, @waune.

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

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:

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

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

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.


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