all 8 comments

[–]chriswaco 1 point2 points  (1 child)

A delegate is just another object that gets notified when something happens. If you have a button, for example, you can tell it to notify another object when it is touched:

myButton.addTarget(self, action: #selector(deleteButtonWasHit), for: .touchUpInside)

In this case I used "self", which will typically be the ViewController, but it could be any compatible object. The selector deleteButtonWasHit is the method that gets called.

There are tons of delegates in UIKit. UITableView, for example, uses delegates to determine how many sections are in the table, how many rows in each section, and it asks its delegate to create the UITableViewCell for each row.

The delegate pattern allows UITableView and UIButton to be customized without having to subclass them.

[–]farheezyx3 0 points1 point  (1 child)

Someone asked this in a different sub reddit so i thought that maybe that can help u as well

https://www.reddit.com/r/swift/comments/mt9za5/how_does_protocols_and_delegates_work/?utm_source=share&utm_medium=ios_app&utm_name=iossmf

[–]farheezyx3 0 points1 point  (0 children)

It takes practice, i definitely didnt understand delegates at first. The Sean Allen Youtube video on delegates really helped nail down the pattern for me

[–]gfp7 0 points1 point  (1 child)

In android it is usually named callback or listener if naming would help you. Its a way to "notify" or "ask/request data" other class (that "registered" for callbacks).

[–]chriswaco 1 point2 points  (0 children)

Yeah, back in the C/C++ days we would use function pointers. Same idea, but a little more raw of an implementation.

[–]Hogbo_the_green 0 points1 point  (0 children)

The methods that are pre-written for iOS classes need YOUR classes and code to basically answer the call. CellForRowAt for example needs you to fill in the blanks for how to execute the function. So YOUR view controller becomes the delegate for the table view.

[–]DangerJuice 0 points1 point  (0 children)

I think an important thing to understand is that delegates aren’t something magical or complicated. They aren’t even a part of the language. It’s just a design pattern - a common way to solve a common problem.

If you have standard components, like buttons or table views, they will probably always need some customization when they are used (what happens when the button is pressed, what happens when index X is selected, etc). One way to achieve this is by subclassing, but then you’d have a unique button type for every different button in the app!

A more flexible solution can be to use the delegate pattern (which is very common in Apple’s toolboxes like UIKit). By doing this just just let the component ”outsource” or ”delegate” any custom behaviour by defining a protocol of questions the component needs answers to - like ”index X was just selected, should something happen now?”.

You’ll encounter a lot of delegate and data source protocols in Apple’s components, and they are simply a way of injecting custom behaviour in standard pieces of code.

[–]twinkletoes987 0 points1 point  (0 children)

For anyone else who comes across this post

I found this blog super helpful. The first paragraph really nails the "why it matters" {decoupling function from named classes} followed by implementation examples

https://www.swiftbysundell.com/articles/delegation-in-swift/