|
I was working on delegate pattern and unable to grasp my mind around it, So here is what i am thinking I create a protocol - basically what i want to do but not defining how
I create a worker class/struct - this creates a reference to protocol, create a function and call the protocol method inside its own mehtod
then in another class , create a reference to this worker class/struct, adopt. the protocol, then implement its method
but i do not understand why i am creating the worker class/struct |
|
Yes, you can. In Paul's UIKit stuff, he frequently does this with delegates such as
(And I apologize for any minor mistakes in the above code; I did it all from memory without bothering to check anything and it's been a while since I did any UIKit work.) |
|
@roosterboy - i was reading the definition of delegation from apple documents, can you please tell me what is the
|
|
So after studying some more material , i understand this much I create a protocol with some functionality
I then create a struct or class that uses or calls the methods of the protocol and provides the data for the methods in protocol.
then if any struct/class adheres to the protocol , that type has to now declare all the methods in the protocol and assign values in the initialiser if possible ...
But what after this... so i have a struct/class that now says i will do the work of defining the functions in protocol, then in the class/struct where say if a button is clicked , then a function is called that in turn uses a function in protocol ... That in turn invokes the struct/class that adopts the protocol and makes it run...
Is that what delegate model is ? Please guide ...
|
|
For further explanation of the delegate pattern this is a good video: https://www.youtube.com/watch?v=DBWu6TnhLeY |
|
@Hatsushira - i saw it earlier , but i totally get lost in the back and forth between views , and delegate here , delegate there , i really need to know what this pattern tries to achieve , can you suggest for eg what is |
|
This depends what you want to achieve. From what I read and what you provided as code example here you want to create a protocol which enables any struct/class which implements it to be able to print. Then you don't need a delegate for this. It would be sufficient to write:
or
Unless I completely misunderstood what you're trying to achieve. |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.