Hey Sean, I bought your iOS Dev course from your site and man I really wish I would’ve bought it sooner. I am a firm believer in the pay to play mentality and your course is just one more reason for that. I’ve just gone through the first five videos and you are very thorough, clear, and straight to the point giving the student everything they will need. I also like that the whole course is done programmatically without a storyboard. I honestly cannot say enough great things about what I’ve experienced so far.
Thanks for the video, Sean! There is a minor issue with this line: var selectionDelegate: SideSelectionDelegate! Should be: weak var selectionDelegate: SideSelectionDelegate? There is no guarantee selectionDelegate will always be non-nil inside SelectionScreen class (unless it's a private property initialized based on a SideSelectionDelegate argument passed in a custom init(delegate: SideSelectionDelegate) {...} method). There are some exceptions when we must indeed use strong delegate properties, but in most cases we should use weak var delegate: SomeDelegate?
i have watched all youtube videos on this topic on youtube. Most of them you fall asleep, or you have some guy trying to give sense to his thoughts. 99,99 % of the times are just pure confusion. Here Sean in 13 minutes goes so straight and clear to the point that even you are a total beginner you will understand it. Impressive. He speaks fast, but i have noticed that you need to keep this peace and speed of words not to get too lost in thoughts. Amazing job man. thanks.
Hey Thomas, thanks for the feedback! I appreciate you guys keeping me honest. Anytime you put your code out there for the world to see, there's bound to be some missteps. To your point, you are correct if you declare your protocol as a class protocol, like `protocol SideSelectionDelegate: class', then you need to declare the delegate variable as 'weak' to avoid retain cycles. However, if don't declare your protocol as 'class' you're allowing your protocol to be adopted by structs, which are value types, and therefore don't really work with reference counting. This blog post provides a good explanation: useyourloaf.com/blog/quick-guide-to-swift-delegates/ . It's a clarification I neglected point out in my video. Thanks for bringing it up!
My first thought (I need to test it, will do it tonight) about this is that we are dealing with class instances and that's why I think there are memory leaks.
OK I tested and I didn't find a strong reference cycle I believe that's because BaseScreen instance doesn't have a property that's referring to the SelectionScreen instance.
Oh my goodness!!! This is the absolute best explanation of Delegates and protocol I have seen on the internet. Thanks so much Seal Allen. You be doing so much for us Swift noobs.
@@seanallen Sean, I’m having this issue of lack of confidence. I just started this journey some months ago band I’m switching from a career in medicine (I’m a Veterinary Doctor with an MSc in Veterinary Physiology but I’ve always loved technology). Sometimes it feels like I can’t just cut it. I’ve always been good academically graduating best in high school and college but coding is just a different ball game. I’ve always been able to understand things really really quickly but some things here just take a while to grasp. Is this normal for everyone? Are there any insights you can share for dealing with this? Sorry for the long read.
Yes, this is normal. Coding is hard to learn at first, but just like any skill, the more you do it the more comfortable you get. I don't have any secret tip or anything. It's all about putting in the time and repetitions (just like any other skill). Then one day, things just start clicking.
Hey, I love your tutorials! I think the speed is fine, but I especially appreciate the fact that you don't waste time launching Xcode and setting up the UI like so many other tutorials out there. You just get to the meat of the matter. Thanks!
+Bill Snebold Thanks, Bill. I hear ya on that. I remember learning myself, and I always hated all the setup on EVERY video. As you've seen, i just do a quick review of my starter project, then on to he good stuff.
Thank you, thank you, thank you... It's amazing how great your analogies are. It really helped me understand the flow of data and how everything works. I was having a moment a few days ago thinking "Maybe Software Engineering isn't for me. I don't understand this stuff and I can't do it. I should honestly give up." Its great to see that there's people out there like you that can articulate in non-technical terms to make understanding this stuff that much easier. Thanks again, you've earned one more subscriber!
I was using a static variable to pass data back and forth yesterday. I knew there had to be a better way, and after watching this video, I immediately went back to my project and changed it to a delegate-protocol pattern. The code is so much cleaner. Thank you so much Sean!
Why is this a better way than passing a variable? I just don't understand... it doesn't seem straight forward, like "beating around the bush." I just don't get, WHY use delegates? Isn't there a simpler way?
I spent the entire afternoon (and now night) tackling the problem of understanding delegates and their application. I've read article after article, downloaded and attempted to analyze projects provided by others, and read official documentation, but everything I've read and seen thus far seems to attempt to explain this concept to people who already understand this concept. Your video here actually explains the concept and relationships very well to someone like myself who is trying to get into coding.
You, Sir, are my hero! I am going through a coding bootcamp and our platform laid out info about protocols and delegates in such dry, confusing manner that I was struggling to understand what goes where until I saw your demo. In less than 10 minutes, everything snapped into place for me! Love the easy to understand and engaging example and love boss/intern analogy. Thank you for sharing your know-how.
I have been struggling with this for about two months now and you just made it so clear for me and I now understand it !! Thank you so much, you sir are a gent and a scholar !!
Sean you are unfolding the most difficult topics of iOS development with so much ease , after your videos everything makes more sense to me .. Thanks Buddy
Sean - love your stuff. It's great when you find someone who thinks in a similar way to you which makes the information transfer fast and effective. Maximum respect so much for taking your time to make these videos.
Thanks Sean, This is the best explanation of delegates by far. The Boss/Intern analogy is exactly what I needed to fully understand the concept. Great Stuff!
10:44 My heart sank when you said “This function is automatically getting called”. That’s the one part I don’t understand about delegates but it was hand-waved away. Every video so far o delegates does this
Great video! I'm currently building an application and this has made my life way easier. I was using notifications and observers. However, I started to notice that my code was getting messy. So I took the time to learn how protocols work and restructured my application. Great decision and good learning experience. Thank you!
This is the best explanation I have seen so far of what delegates do. I do Qt programming (Python) and use delegates all the time for table views but it was always difficult for me to explain the Swift syntax to other people or understand the Swift syntax using delegates as it is a little bit different although the concept is the same. Thanks for the help.
Short, simple and precise. This is the best tutorial on delegate protocol communication pattern I've found as a beginner. Bonus points for referencing Star Wars. Thanks man.
Thank you so much! I could not find any helpful videos explaining protocols. Everyone elses videos weren't clear on how to use them in different situations, your video really helped me understand how to use them in all types of situations.
Nice, concise explanation. In the boss code, "we're safe to force unwrap that because we know we have the UIImage..." @7:35. Someone else taught me "Protect yourself against mistyping a UIImage you know you have by safely unwrapping it..." You would hope you'd catch a runtime error like that during testing, but stuff happens. p.s. I just noticed Ben Sullivan's comment. That would solve this issue.
Thank you so much!! Even after reading bunch of Medium posts, I wasn't clear with the concept of delegates. Thank god I found your life saving video :) Keep making videos, coz you are really good at it!!
HIGHLY RECOMMENDED VIDEO! Just had a question regarding Communication Protocols in my technical interview with a large company today. I knew the whole concept but couldn't put it into words, wish I had watched the whole series earlier. Thanks for the video, Sean.
Keep making Tutorials Sean, you helped me choose my career path and your videos are extremely efficient for learning! Started with your iOS interviews video and now I work as an iOS Developer, dreams do come true and your videos continue to help every time I need to learn!!!
Your video tutorials and the comments left by viewers are very informative. I attended an in person iOS course and couldn't really follow my instructors explanation of this topic. Thank you for such a straight forward explanation, very easy to follow.
Hey, Sean! I was really struggling to understand Communication patterns with Delegate protocol. This example and explanation just made very simple to understand. Im definitely a video learner. Thank you! Take care!
How do you use delegates for the initial screen? where let's say my home screen is a tableview and the moment they click on the cell, the data shows on the next screen? This ex. works for me for second screen binding to first but what about first binding to second? I tried it and I got nil for unwrapping the delegate. Thx for the tutorials they're really good! Gonna support your book!
Man I still sometimes don't get my head to wrap around delegates and protocols concept. But luckily I have this video of yours bookmarked so I always go back to watch it again to make myself clear. Awesome boss and internal anology. Thank you.
Sean, I always appreciate these videos that you produce. They are very enlightening and have helped me a lot in my iOS development. I'm in the process of switching my career from a PeopleSoft HCM developer to an iOS developer and your videos have pushed me further than I could imagine! Thanks for what you do and keep it up!
Sean Allen Truth is that the content was something that I didn’t fully understand despite the courses I have done. Your description gelled immediately so my hats off to you. Happy to have filed that one away for the future.
I’ve watched a video about delegates/protocols every 3 months for the past 2 years and have always tuned out with glazed eyes about halfway through and walked away more confused than when I started. It was always difficult for me to envision the demo use case - but this time, I think I finally get it! (Probably because I was using Singletons and didn’t need to pass data between viewcontrollers - I just updated the global object and then had each VC “refresh” when something else changed a value) But in my current project, I’ve tried to avoid using singletons and instead I’ve been adding a variable named parentVC to all of my selection screens and then having the callingVC set it equal to “self” right before I present it - that way I have a link to the callingVC. But then in the selection screen class, I’d have to attempt to unwrap the parentVC against 2 or 3 different viewcontrollers to figure out which one it was called by and then call a function in it called “didTapWhatever()”. It was messy and felt wrong - but I didn’t know any other way. Now I see how using a delegate simplifies that and makes it much cleaner! Thanks Sean!!!
Perfect!!! Sean, thanks a lot for making this mechanism so clear and easy to understand. Your code and explanations are straightforward and bring it to the point.
Dude, you speak so fast sometimes is hard to keep up but great stuff, finally it's all making more sense to me now! Thanks for sharing your knowledge!!
Yeah, these are my older videos. I’ve worked on that in my new ones. It’s much better. See on the comments in this video... this was probably the worst one, lol
I've seen this kind of stuff declare in this fashion right in the middle of a delegate file: @objc protocol DataDelegate : class { @objc optional func newPrices() @objc optional func newHistory() } does it make any big difference or would you say is pretty much the same principle?
This is declaring the functions as optional. Which means that if you conform to the protocol, you don't HAVE to implement the functions. Where as if you didn't make them optional, and you conformed to the protocol, then implementing the functions would be required.
Nice video! Further extension to this is to have another protocol implemented by Boss so that the communication between Intern and Boss is purely on protocol. For example, Boss would like to pass data (delegate or others) to Intern.
Sean Allen I did have a similar question around the weak reference in the boss VC but managed to see your response. So in short because the protocol wasn’t limited to just class conformance ie it could be adopted by a struct which is a value type this won’t create a retain cycle when adopted by a class or a struct. But if it was limited to classes we must use a weak ref. Sorry if I am repeating myself that’s how we drum home the info lol. Awesome videos though and I’ve subscribed! Nice job!
Great video Sean, thank you ! Subject matter and how you present it (clear, concise) is *very* helpful. I have seen (and used in some cases myself) the delegate/protocol pattern where the 'boss' class maintains a collection/array of delegates instead of just one. This means that it can support one-to-many. I don't see this special one-to-many delegate/protocol case useful for the case of a popup communicating the result of a choice made, but there are some non-UI cases where this may be suitable - I took this approach numerous times in the past instead of the observer/notification pattern as I was porting some event listener logic from Java which made use of Java interfaces and since Java interfaces are very similar to Swift protocols, I latched on to it. Obviously there are some minor differences from your example to support this one-to-many delegate/protocol case, notably logic to support multiple delegates - from having to add/remove multiple delegates, to invoking *all* the delegates' functions when the time comes. I do this by simply iterating over a delegates array, calling each delegate's function. QUESTION: I was wondering if you have see this one-to-many case of delegate/protocol usage and if you know of any pitfalls to doing it this way vs using the observer/notification pattern. Keep up the good work ! Appreciate this video and all the other ones you have made.
Hey crampsalot, I have not seen this in use in the handful of production apps I've worked on. The projects I've been involved in have pretty much stuck to the delegates/protocols for one-to-one and notifications/observers for one-to-many. Unfortunately, i can't really chime in with an opinion here.
You set up your button action to present the selectionVC. When I tried to recreate the program from scratch to practice what I've learned, I did a control-drag from the button to the selection view and got a segue. I put the = self code into the prepare(for: segue...) function. Is there any value in one approach rather than the other? I love the pace and scope of these tutorials.
Sean, I'm so appreciative of the videos! I'd be grateful if you could answer one question though. What if instead of a static Selection Screen, we wanted a dynamic one? For example, let's pretend that our Base Screen has two buttons, a "Choose your side" and a "Choose your favorite character." We want the latter to present Chewbacca and Han Solo. But we want to REUSE the Selection Screen in both cases. How can we setup our Selection Screen to pass in different images, strings, and colors? Do we somehow make the Selection Screen to also be an intern of the Base Screen? Can data go back and forth like this? I'm using your example here, but I have a very real need. I'm making my first app, and I have a screen with a UITextField that takes a number which is needs to pass back to the Base Screen. I can see how I can easily do this with Protocols, as you've explained in your tutorial. BUT... I present this screen from two *different* places on the Base Screen, so I need each Number Selection Screen to be different. I need to pass in three values to my Number Screen: 1) a title (presented with a label), 2) a minimum value of the number, and 3) a maximum value of the number (used later in a function to check the range.) Not knowing any better, I resorted to making these three items properties of the Number Selection Screen View Controller. Then, when I go to the Selection screen, in the same place where we do this: selectionVC.selectionDelegate = self I can add things like: selectionVC.title = "Enter a number" selectionVC.minimum = 1 selectionVC.maximum = 100 But it seems to me my code has gotten ever uglier and more dependent on each other the more I add. Is there a way to do exactly what you're doing in this tutorial, but let is go both ways, so that the Base Screen passes values to the Selection Screen first, then the Selection Screen does exactly what you've shown us in this tutorial, returning the user's selection? Hope that wasn't too wordy for you. Wanted to be clear.
To be honest with you Jay, I'm not sure if I followed all that to know EXACTLY what you're looking for in order to provide a correct answer. It's tough to answer these types of questions via UA-cam comments. I do offer 1-on-1 video call consulting/coaching to review code. If your interested in that, DM me on twitter @SeanA0400 and we can discuss the details.
Dear Sean, Sorry I am not a native English speaker and I did not fully understand what you mean at 9:21. Can you explain in other way the line 24 what exactly means selectionVC.selectionDelegate = self. I understand that selectionVC.selectionDelegate is variable, but equals to self. How to understand this? Does it means BaseScreen also now has variable selectionDelegate?
Hey Raimonds, I'm happy to explain in another way. Hope this helps. - selectionVC is an object we create on line 23. This is where we create the instance of SelectionScreen that we will present. - selectionVC has a property that we created as a variable on the SelectionScreen called selectionDelegate - When we use 'selectionVC.selectionDelegate', we are actually setting the variable on the SelectionScreen before presenting it. - On line 24, we set selectionVC.selectionDelegate = self. Another way to say this would be selectionVC.selectionDelegate = BaseScreen. (Because we are on the BaseScreen at this point, self = BaseScreen). - To clarify, BaseScreen DOES NOT have a variable called selection delegate. I hope that helped. If you still need clarification, please feel free to ask. We'll figure this out.
Sean Allen Thank you so much for helping us to understand Swift. I think it gets more clear now, but I have some questions more and if you are not against I will write you. :) Once again I really appreciate your help.
I wish you made videos like this for MacOS development too. Very clear. I know most of this is similar, but I get some errors that just doesn't make sense.
Dear Allen , I’ve enrolled in your basic course , getting the hang of it . Step by step . Question - can we create an app for our employees in office to check in and out during office hours and indicate the world load they have done for the day ?
Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - ua-cam.com/video/MSIe2y6Fee8/v-deo.html
Hey Sean, I bought your iOS Dev course from your site and man I really wish I would’ve bought it sooner. I am a firm believer in the pay to play mentality and your course is just one more reason for that. I’ve just gone through the first five videos and you are very thorough, clear, and straight to the point giving the student everything they will need. I also like that the whole course is done programmatically without a storyboard. I honestly cannot say enough great things about what I’ve experienced so far.
Thanks for the video, Sean! There is a minor issue with this line:
var selectionDelegate: SideSelectionDelegate!
Should be:
weak var selectionDelegate: SideSelectionDelegate?
There is no guarantee selectionDelegate will always be non-nil inside SelectionScreen class (unless it's a private property initialized based on a SideSelectionDelegate argument passed in a custom init(delegate: SideSelectionDelegate) {...} method).
There are some exceptions when we must indeed use strong delegate properties, but in most cases we should use weak var delegate: SomeDelegate?
The "intern" and "boss" analogy is so helpful, keep these videos coming!
Glad you enjoyed it, Erica! I've always been pretty good at coming up with analogies to explain things... it's a fun way to teach.
this is the first analogy for protocols/delegates that finally made me understand the concept. Thank you Mr. Allen.
agree
Finally someone who values my time so that I don't have to speed up the videos. Keep it up.
i have watched all youtube videos on this topic on youtube. Most of them you fall asleep, or you have some guy trying to give sense to his thoughts. 99,99 % of the times are just pure confusion. Here Sean in 13 minutes goes so straight and clear to the point that even you are a total beginner you will understand it. Impressive. He speaks fast, but i have noticed that you need to keep this peace and speed of words not to get too lost in thoughts. Amazing job man. thanks.
You have to declare your delegate var as weak to avoid reference cycles. This is one common question to interviews too..
Hey Thomas, thanks for the feedback! I appreciate you guys keeping me honest. Anytime you put your code out there for the world to see, there's bound to be some missteps. To your point, you are correct if you declare your protocol as a class protocol, like `protocol SideSelectionDelegate: class', then you need to declare the delegate variable as 'weak' to avoid retain cycles. However, if don't declare your protocol as 'class' you're allowing your protocol to be adopted by structs, which are value types, and therefore don't really work with reference counting. This blog post provides a good explanation: useyourloaf.com/blog/quick-guide-to-swift-delegates/ . It's a clarification I neglected point out in my video. Thanks for bringing it up!
Thanks for your answer Sean. To tell you the truth I wasn't aware of that..
I had the exact same question when I began watching this video, glad I scrolled through the comments to find your reply Sean :)
My first thought (I need to test it, will do it tonight) about this is that we are dealing with class instances and that's why I think there are memory leaks.
OK I tested and I didn't find a strong reference cycle I believe that's because BaseScreen instance doesn't have a property that's referring to the SelectionScreen instance.
one of the best iOS teacher on this planet. Good job Sean.
Thanks for the kind words, Amit.
Oh my goodness!!! This is the absolute best explanation of Delegates and protocol I have seen on the internet. Thanks so much Seal Allen. You be doing so much for us Swift noobs.
Glad it was helpful, Mark!
@@seanallen Sean, I’m having this issue of lack of confidence. I just started this journey some months ago band I’m switching from a career in medicine (I’m a Veterinary Doctor with an MSc in Veterinary Physiology but I’ve always loved technology).
Sometimes it feels like I can’t just cut it. I’ve always been good academically graduating best in high school and college but coding is just a different ball game.
I’ve always been able to understand things really really quickly but some things here just take a while to grasp.
Is this normal for everyone?
Are there any insights you can share for dealing with this?
Sorry for the long read.
Yes, this is normal. Coding is hard to learn at first, but just like any skill, the more you do it the more comfortable you get. I don't have any secret tip or anything. It's all about putting in the time and repetitions (just like any other skill). Then one day, things just start clicking.
Hey, I love your tutorials! I think the speed is fine, but I especially appreciate the fact that you don't waste time launching Xcode and setting up the UI like so many other tutorials out there. You just get to the meat of the matter. Thanks!
+Bill Snebold Thanks, Bill. I hear ya on that. I remember learning myself, and I always hated all the setup on EVERY video. As you've seen, i just do a quick review of my starter project, then on to he good stuff.
Thank you, thank you, thank you... It's amazing how great your analogies are. It really helped me understand the flow of data and how everything works. I was having a moment a few days ago thinking "Maybe Software Engineering isn't for me. I don't understand this stuff and I can't do it. I should honestly give up." Its great to see that there's people out there like you that can articulate in non-technical terms to make understanding this stuff that much easier. Thanks again, you've earned one more subscriber!
I was using a static variable to pass data back and forth yesterday. I knew there had to be a better way, and after watching this video, I immediately went back to my project and changed it to a delegate-protocol pattern. The code is so much cleaner. Thank you so much Sean!
Happy to clear that up, Ryan!
Why is this a better way than passing a variable? I just don't understand... it doesn't seem straight forward, like "beating around the bush." I just don't get, WHY use delegates? Isn't there a simpler way?
I spent the entire afternoon (and now night) tackling the problem of understanding delegates and their application. I've read article after article, downloaded and attempted to analyze projects provided by others, and read official documentation, but everything I've read and seen thus far seems to attempt to explain this concept to people who already understand this concept. Your video here actually explains the concept and relationships very well to someone like myself who is trying to get into coding.
Happy to hear you found it helpful, M 👍
You, Sir, are my hero! I am going through a coding bootcamp and our platform laid out info about protocols and delegates in such dry, confusing manner that I was struggling to understand what goes where until I saw your demo. In less than 10 minutes, everything snapped into place for me! Love the easy to understand and engaging example and love boss/intern analogy. Thank you for sharing your know-how.
Happy to hear it helped you, Lily. I struggled with Delegates/Protocols for the first 6 months of my career, so I know how ya feel!
I have been struggling with this for about two months now and you just made it so clear for me and I now understand it !! Thank you so much, you sir are a gent and a scholar !!
That's awesome to hear, Neil. Happy to help!
One of the best iOS instructor! This tutorial helped me understand after countless hours understanding protocols and delegates!
I appreciate the kind words, Dorian 🙌
Sean you are unfolding the most difficult topics of iOS development with so much ease , after your videos everything makes more sense to me .. Thanks Buddy
Glad you found it useful, Hanzula. I've got a lot more videos 😀
This whole video was a "Aha" moment for me! Your videos are really good for reviewing concepts! Please keep uploading them :)
+Mashfique Anwar Thanks! More on the way!
I love your pacing. Straight to the point. And fast. Every time. Well done.
Clearest explanation of delegates in Swift I’ve ever found. Thanks Sean!
Thanks! Glad you enjoyed it 😀
Sean - love your stuff. It's great when you find someone who thinks in a similar way to you which makes the information transfer fast and effective. Maximum respect so much for taking your time to make these videos.
Thanks for the kind words, Christian! Motivates me to keep going!
Thanks Sean, This is the best explanation of delegates by far. The Boss/Intern analogy is exactly what I needed to fully understand the concept. Great Stuff!
Thanks Mauricio! Glad it helped. Appreciate you sharing via tweet as well.
10:44 My heart sank when you said “This function is automatically getting called”. That’s the one part I don’t understand about delegates but it was hand-waved away. Every video so far o delegates does this
Great video!
I'm currently building an application and this has made my life way easier.
I was using notifications and observers. However, I started to notice that my code was getting messy.
So I took the time to learn how protocols work and restructured my application.
Great decision and good learning experience.
Thank you!
Happy to hear you found the video helpful, Sergio 😀
This is the best explanation I have seen so far of what delegates do. I do Qt programming (Python) and use delegates all the time for table views but it was always difficult for me to explain the Swift syntax to other people or understand the Swift syntax using delegates as it is a little bit different although the concept is the same. Thanks for the help.
Glad to hear the video helped out, Tim!
Short, simple and precise. This is the best tutorial on delegate protocol communication pattern I've found as a beginner. Bonus points for referencing Star Wars. Thanks man.
One of the best Delegates/Protocol tutorials I've seen. Thanks
Much appreciated Bahe! Happy to hear the video helped!
Thank you so much! I could not find any helpful videos explaining protocols. Everyone elses videos weren't clear on how to use them in different situations, your video really helped me understand how to use them in all types of situations.
Happy to hear it helped, Steve!
Nice, concise explanation.
In the boss code, "we're safe to force unwrap that because we know we have the UIImage..." @7:35. Someone else taught me "Protect yourself against mistyping a UIImage you know you have by safely unwrapping it..." You would hope you'd catch a runtime error like that during testing, but stuff happens.
p.s. I just noticed Ben Sullivan's comment. That would solve this issue.
The best explanation I have seen so far. Great content Sean!
Thanks for the kind words, Arnav 😀
So close to 100k subscribers. Congratulations in advance, Sean!
Thanks James!
Thank you so much!! Even after reading bunch of Medium posts, I wasn't clear with the concept of delegates. Thank god I found your life saving video :) Keep making videos, coz you are really good at it!!
Thanks Satyam! Glad it helped you out! I know this is a tricky topic.
I just had my "aha!" moment while watching this video. Super helpful, Sean. Thanks for these videos and for the great podcast.
Awesome! Love those moments 😀
HIGHLY RECOMMENDED VIDEO!
Just had a question regarding Communication Protocols in my technical interview with a large company today.
I knew the whole concept but couldn't put it into words, wish I had watched the whole series earlier.
Thanks for the video, Sean.
Glad it was helpful... although a little too late it sounds like. 😀
Keep making Tutorials Sean, you helped me choose my career path and your videos are extremely efficient for learning! Started with your iOS interviews video and now I work as an iOS Developer, dreams do come true and your videos continue to help every time I need to learn!!!
That's awesome to hear Adam! Congrats on the new job!
Thank you so much for making this tutorial! I was struggling for hours but then I watched your video and got my code working shortly after.
Happy to hear it was helpful to you project!
Your video tutorials and the comments left by viewers are very informative. I attended an in person iOS course and couldn't really follow my instructors explanation of this topic. Thank you for such a straight forward explanation, very easy to follow.
Thanks Srav! Glad you are enjoying the content!
Enjoy watching your videos. They're presented in a clear, logical way that makes it easy for me to understand. Thank you!
Glad you enjoy them, Phontaine!
One day i asked god to teach me iOS and he had sent you... Stay blessed @Sean Allen
Thanks Lalitha!
Hey, Sean! I was really struggling to understand Communication patterns with Delegate protocol. This example and explanation just made very simple to understand. Im definitely
a video learner. Thank you! Take care!
Glad the video helped you out, Marcos!
How do you use delegates for the initial screen? where let's say my home screen is a tableview and the moment they click on the cell, the data shows on the next screen? This ex. works for me for second screen binding to first but what about first binding to second? I tried it and I got nil for unwrapping the delegate. Thx for the tutorials they're really good! Gonna support your book!
Man I still sometimes don't get my head to wrap around delegates and protocols concept. But luckily I have this video of yours bookmarked so I always go back to watch it again to make myself clear. Awesome boss and internal anology. Thank you.
Your teaching method is amazing, loved the video.
Thanks for the kind words, Hiba 😀
Great analogy that clears the air on how delegates work.
Happy it helped, Pavan!
This was the first time i read all the comments of a video , they were very informative thanks for replying all the questions sir :-)
I can't always get to all of them (some comments are very detailed questions), but I try to get to as many as I can. Happy you appreciate it!
Sean, I always appreciate these videos that you produce. They are very enlightening and have helped me a lot in my iOS development. I'm in the process of switching my career from a PeopleSoft HCM developer to an iOS developer and your videos have pushed me further than I could imagine! Thanks for what you do and keep it up!
That's awesome to hear, Trevor! Thanks for the motivating words. Happy to hear I can help. Keep at it!
It's an oldie but a goodie. Great stuff Sean.
Always fun to go back and revisit 😀
Sean Allen
Truth is that the content was something that I didn’t fully understand despite the courses I have done. Your description gelled immediately so my hats off to you. Happy to have filed that one away for the future.
Many of the topics in programming and iOS development will take repeated study. Glad it was helpful!
I’ve watched a video about delegates/protocols every 3 months for the past 2 years and have always tuned out with glazed eyes about halfway through and walked away more confused than when I started. It was always difficult for me to envision the demo use case - but this time, I think I finally get it! (Probably because I was using Singletons and didn’t need to pass data between viewcontrollers - I just updated the global object and then had each VC “refresh” when something else changed a value)
But in my current project, I’ve tried to avoid using singletons and instead I’ve been adding a variable named parentVC to all of my selection screens and then having the callingVC set it equal to “self” right before I present it - that way I have a link to the callingVC. But then in the selection screen class, I’d have to attempt to unwrap the parentVC against 2 or 3 different viewcontrollers to figure out which one it was called by and then call a function in it called “didTapWhatever()”. It was messy and felt wrong - but I didn’t know any other way.
Now I see how using a delegate simplifies that and makes it much cleaner! Thanks Sean!!!
Been struggling with this for a while now, finally makes sense. Thanks Sean!
Happy to help, Vu 👍
great tutorial ... i am new in IOS development .. your tutorials are really helpful , keep making videos for beginners
Thanks Charmi! Glad you enjoyed it.
This video still saves lives one year after its release...
Hehe, yeah, it's one of my most viewed videos.
Great video! I finally get the idea of the protocols and delegates. Analogy worked really well. Thank you.
The boss vs intern analogy is genius!
Another one of your videos helping me out of a bind!! Thank you!
Happy to help, Chris 😀
Perfect!!! Sean, thanks a lot for making this mechanism so clear and easy to understand. Your code and explanations are straightforward and bring it to the point.
Just came across this video again... Wow, just over 2 years since you posted it! Still love the "intern and boss" analogy! 💯
It's a classic... I talk insanely fast... but other than that, I like it.
Yes, these things are evergreen
Superb.. You just put an end to my 3 weeks of stress...! Thanks...
Happy to hear it helped, Kristoffer 👍
watching all the videos, got three interview this Saturday.
Good luck! Hope they help.
Thanks Sean. Your videos are amazing. Took a dose of your videos and went high to interview.
Very nice videos. I like your speed. Very logically explained and very supportive analogies.
Thanks Timo! Glad you enjoyed the video!
Thanks for the clear and crispy explaining protocol.
Glad you liked it!
Thank you boss (Sean Allen). I (intern/delegate) will implement this lesson right away!
Dude, you speak so fast sometimes is hard to keep up but great stuff, finally it's all making more sense to me now! Thanks for sharing your knowledge!!
Yeah, these are my older videos. I’ve worked on that in my new ones. It’s much better. See on the comments in this video... this was probably the worst one, lol
I've seen this kind of stuff declare in this fashion right in the middle of a delegate file:
@objc protocol DataDelegate : class {
@objc optional func newPrices()
@objc optional func newHistory()
}
does it make any big difference or would you say is pretty much the same principle?
This is declaring the functions as optional. Which means that if you conform to the protocol, you don't HAVE to implement the functions. Where as if you didn't make them optional, and you conformed to the protocol, then implementing the functions would be required.
Love the way you explain, please keep it up!
Thanks Simon! More videos on the way!
the tutorial was too good, watched a couple before this one, but only this made sense to me:)
Glad it finally makes sense now, Akash!
Simply great explanation with rapid speed and a good example(intern - boss)
Thanks Govind! Glad it was helpful👍
omg, I finally understood how it works, thank you very much!
Glad to help, Dosbol!
Love your to the point coding videos
Thanks Javad! I do my best to eliminate all the irrelevant stuff.
Great video bro, you're such a good teacher!
+Leonardo Emilio Thanks for the kind words! Glad you enjoyed the video.
You explain really well, Sean. Great video, thanks a lot.
Glad you liked it!
Really great analogy and easy to understand! Love ur channel! I am your observer now 😄
Glad you enjoyed it, Aldo!
Nice video! Further extension to this is to have another protocol implemented by Boss so that the communication between Intern and Boss is purely on protocol. For example, Boss would like to pass data (delegate or others) to Intern.
Thanks for the kind words, D Chu! And you're correct, you could extend this implementation that way.
Have a question about Delegates/Protocols? Leave a comment, I'm happy to help!
Sean Allen I did have a similar question around the weak reference in the boss VC but managed to see your response. So in short because the protocol wasn’t limited to just class conformance ie it could be adopted by a struct which is a value type this won’t create a retain cycle when adopted by a class or a struct. But if it was limited to classes we must use a weak ref. Sorry if I am repeating myself that’s how we drum home the info lol. Awesome videos though and I’ve subscribed! Nice job!
+Alex McPherson Yup. You got it!
Sean Allen awesome thanks man!
you speak so fast
I am new to Swift so sorry if this question sounds stupid, but what is a delegate
Your videos are so helpful Sean! Just wanted to say thank you
Thanks! Glad you enjoy them 👍
This is by far the best video I found explaining protocols. Thanks Sean and love your interview question videos.
That's awesome to hear, John! Glad you're enjoying the interview question videos series.
Best video on this topic by far. Thank you
Such a great video. Very informative and fast paced (in a good way).
Thanks Laurent! Glad you enjoyed the video and pace!
I am actually an intern learning swift. I am here after my Trainer sent me to learn communication patterns.
Thank you Sean, your explanation was great !
Glad it was helpful!
Great video Sean, thank you ! Subject matter and how you present it (clear, concise) is *very* helpful.
I have seen (and used in some cases myself) the delegate/protocol pattern where the 'boss'
class maintains a collection/array of delegates instead of just one. This means that it can support
one-to-many.
I don't see this special one-to-many delegate/protocol case useful for the case of a popup communicating
the result of a choice made, but there are some non-UI cases where this may be suitable - I took this
approach numerous times in the past instead of the observer/notification pattern as I was porting some
event listener logic from Java which made use of Java interfaces and since Java interfaces are very similar
to Swift protocols, I latched on to it.
Obviously there are some minor differences from your example to support this one-to-many delegate/protocol
case, notably logic to support multiple delegates - from having to add/remove multiple delegates, to
invoking *all* the delegates' functions when the time comes. I do this by simply iterating over a delegates
array, calling each delegate's function.
QUESTION: I was wondering if you have see this one-to-many case of delegate/protocol usage and if you know
of any pitfalls to doing it this way vs using the observer/notification pattern.
Keep up the good work ! Appreciate this video and all the other ones you have made.
Hey crampsalot, I have not seen this in use in the handful of production apps I've worked on. The projects I've been involved in have pretty much stuck to the delegates/protocols for one-to-one and notifications/observers for one-to-many. Unfortunately, i can't really chime in with an opinion here.
Sean Allen thank you - appreciate the response.
Great video!! Best protocol explain I found.
One question, in 9:02, why selectionVC got selectionDelegata property ?
The SelectionScreen has a variable called selectionDelegate. Here at 9:02 we are setting that variable to the current view controller.
You set up your button action to present the selectionVC. When I tried to recreate the program from scratch to practice what I've learned, I did a control-drag from the button to the selection view and got a segue. I put the = self code into the prepare(for: segue...) function. Is there any value in one approach rather than the other?
I love the pace and scope of these tutorials.
Hey Cometmace, they are basically the same. One is a storyboard way of doing it, and the other is just doing it in code. You get the same result.
Sean, I'm so appreciative of the videos! I'd be grateful if you could answer one question though.
What if instead of a static Selection Screen, we wanted a dynamic one? For example, let's pretend that our Base Screen has two buttons, a "Choose your side" and a "Choose your favorite character." We want the latter to present Chewbacca and Han Solo. But we want to REUSE the Selection Screen in both cases.
How can we setup our Selection Screen to pass in different images, strings, and colors? Do we somehow make the Selection Screen to also be an intern of the Base Screen? Can data go back and forth like this?
I'm using your example here, but I have a very real need. I'm making my first app, and I have a screen with a UITextField that takes a number which is needs to pass back to the Base Screen. I can see how I can easily do this with Protocols, as you've explained in your tutorial. BUT... I present this screen from two *different* places on the Base Screen, so I need each Number Selection Screen to be different. I need to pass in three values to my Number Screen: 1) a title (presented with a label), 2) a minimum value of the number, and 3) a maximum value of the number (used later in a function to check the range.)
Not knowing any better, I resorted to making these three items properties of the Number Selection Screen View Controller. Then, when I go to the Selection screen, in the same place where we do this:
selectionVC.selectionDelegate = self
I can add things like:
selectionVC.title = "Enter a number"
selectionVC.minimum = 1
selectionVC.maximum = 100
But it seems to me my code has gotten ever uglier and more dependent on each other the more I add.
Is there a way to do exactly what you're doing in this tutorial, but let is go both ways, so that the Base Screen passes values to the Selection Screen first, then the Selection Screen does exactly what you've shown us in this tutorial, returning the user's selection?
Hope that wasn't too wordy for you. Wanted to be clear.
To be honest with you Jay, I'm not sure if I followed all that to know EXACTLY what you're looking for in order to provide a correct answer. It's tough to answer these types of questions via UA-cam comments. I do offer 1-on-1 video call consulting/coaching to review code. If your interested in that, DM me on twitter @SeanA0400 and we can discuss the details.
Can two views be delegates of each other? Yes, that's possible.
As soon as I see its a video made by Sean it gets the like.
Dear Sean, Sorry I am not a native English speaker and I did not fully understand what you mean at 9:21. Can you explain in other way the line 24 what exactly means selectionVC.selectionDelegate = self. I understand that selectionVC.selectionDelegate is variable, but equals to self. How to understand this? Does it means BaseScreen also now has variable selectionDelegate?
Hey Raimonds, I'm happy to explain in another way. Hope this helps.
- selectionVC is an object we create on line 23. This is where we create the instance of SelectionScreen that we will present.
- selectionVC has a property that we created as a variable on the SelectionScreen called selectionDelegate
- When we use 'selectionVC.selectionDelegate', we are actually setting the variable on the SelectionScreen before presenting it.
- On line 24, we set selectionVC.selectionDelegate = self. Another way to say this would be selectionVC.selectionDelegate = BaseScreen. (Because we are on the BaseScreen at this point, self = BaseScreen).
- To clarify, BaseScreen DOES NOT have a variable called selection delegate.
I hope that helped. If you still need clarification, please feel free to ask. We'll figure this out.
Sean Allen Thank you so much for helping us to understand Swift. I think it gets more clear now, but I have some questions more and if you are not against I will write you. :)
Once again I really appreciate your help.
Feel free to reach out via email, Raimonds.
Thank you so much for your help!
Brilliant explanation!
Glad you got value out of it, Chris!
Everytime i start to watching your video, i think the video speed is 1.25... Every single time i fall in this mistake haha
Haha, I've been working on slowing down a bit. Thanks for watching!
yea, no more Sean vids for me....sounds like a gravel hauler droning down the road.
Putting 50% playback speed makes him sound drunk lol
I am struggling too haha and on top of that i am a german native speaker which makes it even tougher but great video - thanks for explaning 👍🏻
I wish you made videos like this for MacOS development too. Very clear. I know most of this is similar, but I get some errors that just doesn't make sense.
I haven't done any Mac development as of yet. Just iOS
Ok. Would be nice to hear if you ever want to start making some simple apps for the Mac! Swift is extremely similar for both!
really good explanation thanks Sean!
Thanks Max!
i dont usually comment but when i do it's because its a great video!
Glad you enjoyed it!
Nice video! Easy to understand
Glad you enjoyed it, Alan!
Still helpful in 2020!
Thanks Sean.
Happy to help 😀
Best video on this topic I've seen so far. Thank you!
Glad it was helpful!
Love how you explain code, good job!
Thanks!
Nice and simple explanation. Thanks!
Thanks Martin. Glad you enjoyed it!
Thanks dude. It helped me understanding it much better.
I just had an exam in Swift where I was asked about delegates and couldn't answer.🙁
Happy to help Mohammed!
you are amazing bro , please come back to swift , we need you
Soon...
Great video! how would I apply this to enabling a button on the first/intern view controller? I keep trying different things with no luck
I would "like" this video five times if I could.
Thanks nipponese. Glad you enjoyed it 😎
best explaintion of delegate and protocols 👌👌👌❤❤❤
I have an updated better version on my channel here - ua-cam.com/video/qiOKO8ta1n4/v-deo.html
@@seanallen thanks but i have already watched that, but didn't got the point before
i'll watch that again.
✌
Hi Sean. Thanks you made it very clear. Keep it up
+Klaus Jakobsen Glad you enjoyed it, Klaus!
Very useful, simple and easy to understand video, thanks!
Dear Allen , I’ve enrolled in your basic course , getting the hang of it . Step by step .
Question - can we create an app for our employees in office to check in and out during office hours and indicate the world load they have done for the day ?