V
V
Vladislav Resley2020-05-25 14:43:52
iOS
Vladislav Resley, 2020-05-25 14:43:52

What is a delegate and why is it needed in IOS application development?

I have already broken my whole head, please explain what delegates are in the application, what they are for and what their meaning is, I also ask you to explain what AppDelegate is and what it is for. I ask you to explain everything in the simplest possible language, because I just recently started learning Swift, that's the maximum for dummies. Thank you task

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
iMaximus, 2020-05-25
@iMaximus

I suggest you learn google first. The ability to compose search queries is very important in our profession :)
What is the AppDelegate for?

B
briahas, 2020-05-26
@briahas

Vladislav Resli (it seems to me - If you have studied C # before, then this knowledge can play a cruel joke on you in understanding delegates in iOS development. Why? Because, unlike C # in iOS development, they are declarative. They are not behind them special constructions and declarations.)
If you have created the "Viewer" and "Player" classes and you need the "viewer" to know about the playback end event, then you create a property with the "User" type in the "Player" class so that, when necessary, call the required method on this property (let it be didEndPlaying()).
It turns out that, logically, the player delegates to the user to perform actions on a specific event. It turns out (in my example), the viewer is the delegate of the player. And in order to immediately understand this logical chain when viewing the code, it is customary to name such properties delegate(in my case, such a property will be in the Player class).
And since it’s not right to address a specific class, but they create the “Player Delegate” protocol (in English it would be PlayerDelegate, it sounds more familiar, don’t [AppDelegate, etc.]) which anyone who wants to be a Player delegate will have to satisfy.
the code:

weak var delegate: PlayerDelegate?,

...
    private func onReachingEnd() {
        delegate?.didEndPlaying()
    }

But why the delegate should be weak and optional, I leave it to you to find out for yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question