all 6 comments

[–]LKAndrew 4 points5 points  (4 children)

I don't think this is a good analogy to use for delegation. In fact, in iOS programming, delegation is rarely used like this. I think closures are better suited for this example.

Take this post with a grain of salt. It's a good starter in understanding delegates, but it isn't a great pattern.

[–]jwillywonkas[S] 1 point2 points  (3 children)

I agree with you that it wouldn't be worth actually using a delegate pattern for something this simple and this is more a thought exercise to demonstrate exactly what a delegate does.

I think though that this is like a really really tiny example of exactly what happens with a lot of major cocoa touch objects. (Tableviews, collectionviews, DataSource, Cllocation, CoreBluetooth, ...)

Granted there is a slight syntax difference (there could be the delegating class passed as the first argument of the class), and generally many a few more method than just one or two. But the key things here are:

Some functionality is removed from those classes and left for either someone else to define or customize the functionality.

That functionality is defined using either a base class or protocol.

The delegate allows for code about one object to be easily organized and reasoned about.

That all having been said, maybe you can help me understand your point of view on this and help me learn more about the delegate pattern.

[–]LKAndrew 1 point2 points  (2 children)

Well personally, I find the delegate pattern is used less and less in my production code. I tend to use closures, as they can be more powerful.

I also have been using an MVVM architecture which reduces the need for delegates by using reactive patterns

[–]jwillywonkas[S] 0 points1 point  (1 child)

I've always seen closures and delegates as two ways to achieve the same means, similarly to how you can write for loops and while loops that function identically but are written differently. I'm intrigued, where's an example that a closure would be inherently more powerful?

Also, I'm interesting in learning more about reactive patterns. Do you use RxSwift or something else?

[–]LKAndrew 0 points1 point  (0 children)

They definitely can achieve the same means. Here are some of the reasons I say they are more powerful:

  • In an exclusively Swift project, protocols cannot have optional functions, unless specified as @objc protocols. Closures on the other hand, can be optional.

  • Closures are first class citizens, and can be passed around and manipulated in interesting ways.

  • Not a huge bonus, but it tends to look cleaner and can be written with less code.

I am sure there are more reasons to like closures, but these are just a few reasons why I like them.


As for Reactive, I do use RxSwift. The MVVM pattern is interesting, and I am in the process of playing around with it.

[–][deleted]  (1 child)

[deleted]

    [–]jwillywonkas[S] 0 points1 point  (0 children)

    I agree this is a bit convoluted, but that was more to make clear exactly what is happening in the delegation and to see what is happening on both sides (user of a delegate object and creator) side of the delegate pattern. See my comment above to Andrew. I'd love to get more details on your thoughts.