Chapter 13: Delegates and Protocols

Hello, I am new in Swift, I don’t understand why are in protocol AdditemViewControllerDelegate used 2 different ways how to define function:


Why it is not same, for example:
func addItemViewControllerDidCancel(_ controller: AddItemViewController)
func itemDetailViewControllerDidAdd(_ controller: ItemDetailViewController, of item: ChecklistItem)

I didn’t find explanation in iOS Apprentice. Is it some kind of best practice? Could anybody explain it to me please?

The two method signatures are different because one has only one parameter (a reference to the AddItemViewController instance) and the second has two parameters (a reference to the AddItemViewController instance and the just added item). If you had only one parameter for the second one, then both method signatures would look much closer …

Not sure if that explains things for you or if your confusion arises from some other point? If that didn’t clarify things, could you explain a bit more in detail as to what confuses you about the two delegate methods?

Thanks for your answer. But I am still not fully sure, why it is like that. In Swift Apprentice book, there is chapter about functions and even two parameters functions are written as:

func name(parameter 1, parameter 2)

So I am little bit confused why you don’t use just:

func addItemViewControllerDidAdd(_ controller: ItemDetailViewController, _ item: ChecklistItem)

and combine these two approaches. Is it just some programmer benefit or rule?

If you define the method as:

func addItemViewControllerDidAdd(_ controller: ItemDetailViewController, _ item: ChecklistItem)

Then when you call the method, you will have your code something like this:

addItemViewControllerDidAdd(self, x)

And that doesn’t really tell anybody else who is reading the code what x is since there is no context/clue as to what that might be when you have two parameters - is the first one the view controller, or is the second one the view controller? It’s not easy to tell (though you can guess that the first probably is the view controller, another developer can always reverse those two …)

With the method naming as addItemViewController(_:didFinishAdding:) it’s much more clearer as to what the parameters are as well as what the action carried out here was.

Of course, if you want to do it the other way, there is nothing stopping you :slight_smile: But naming methods in in a way that makes the action as well as the parameters clear, while keeping the code itself concise is what I personally prefer to do. So, it’s also a matter of preference - there’s no right or wrong way. You can go with whatever option you personally prefer …

Now it is clear! :slight_smile: I wasn’t my target to contradict anything, I have just wanted to understand, if this is only one way and why it is like that. Thanks for your patience :wink:!

No worries :slight_smile: I’m glad to hear that things are clearer now. If you have any other questions, don’t hesitate to post here.

Hey there,

I don’t know where to suggest, but really need a great tutorial for Audio carPlay!

Thanks for all your great works :slight_smile:

@idevelopper Thank you for your suggestion - much appreciated! I will send it to the tutorial team. Thanks again!

This topic was automatically closed after 166 days. New replies are no longer allowed.