Swift Delegate Protocol Pattern Tutorial 2023 | iOS Communication Patterns

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ย. 2024
  • Head to squarespace.co... to save 10% off your first purchase of a website or domain using code SEANALLEN.
    This video explains the Delegate Protocol Communication Pattern in Swift which is how two views communicate in a one-to-one manner. This is a fundamental question in iOS development and I was asked about it a lot during my interviews. In this tutorial I'll walk you through how the delegate protocol pattern in Swift works, using Xcode 14.
    This is part of a series of videos about iOS interview questions in Swift. Check out the overview where I list and briefly discuss all the topics that are in this series:
    • iOS Interview Question...
    Project Source Code:
    www.dropbox.co...
    iOS Dev Courses:
    seanallen.teac...
    Twitter:
    Sean Allen - / seanallen_dev
    Hired.com:
    hired.com/x/1n01g
    Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
    Paul Hudson's Hacking With Swift:
    gumroad.com/a/...
    Donny Wals - Combine:
    gumroad.com/a/...
    Mark Moeyken’s SwiftUI Books:
    www.bigmountainstudio.com/swiftui-views-book/fzc51
    Objc.io Books (Thinking in SwiftUI & Advanced Swift):
    gumroad.com/a/...
    Ray Wenderlich Books:
    store.raywende...
    #swift #softwaredeveloper #iosdeveloper

ความคิดเห็น • 108

  • @seanallen
    @seanallen  ปีที่แล้ว +3

    Learn more with my iOS Development courses - seanallen.teachable.com

    • @Хумо-х7з
      @Хумо-х7з ปีที่แล้ว

      Where are you calling presentProductSelectionVC?

  • @tzuuyaolee9402
    @tzuuyaolee9402 ปีที่แล้ว +5

    the "job description" concept is on point. thanks.

    • @seanallen
      @seanallen  ปีที่แล้ว

      Glad it helped!

  • @marcx3827
    @marcx3827 ปีที่แล้ว +5

    By far! The best explanation of this topic. Especially the hint that this functinons are getting fired automatically in a lot of framworks. Great job Sean!

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      That was the key piece of the puzzle for me. Once I understood that, delegates made sense to me.

  • @numb980
    @numb980 ปีที่แล้ว +11

    To prevent strong reference cycles, delegates are declared as weak references (Swift Doc)
    In your case this kinda works only because you don't hold a reference to destination. But in real world, in 99% of situations, the owner holds a reference to its delegate, and a delegate has its owner. Hence why you should always use weak, can't go wrong

    • @seanallen
      @seanallen  ปีที่แล้ว +5

      Many use cases require a weak reference and I should have mentioned that. That is a fair point and I accept that criticism. 99% is a bit much tho. The way I demonstrated delegates is how I write them the vast majority of the time when communicating between two views. I would also argue that a developer understanding when and where to use "weak" is the better than adding weak to everything just to be safe.

    • @ebubeawachie
      @ebubeawachie ปีที่แล้ว +1

      ​@@seanallen Besides using weak could lead to issues when the delegate methods are being called from within an escaping closure right? Because the closure could be deallocated? Or am I off my rocker?

    • @philipps19
      @philipps19 ปีที่แล้ว

      @@seanallen Just accept that you've been wrong and go forward. You don't need to write 'weak' everywhere, but as @numb5206 said, delegate and escaping closures situations are 99% must have.

  • @EduardoEscaleira
    @EduardoEscaleira ปีที่แล้ว

    Nicely done! In the first 7 seconds of your video I already knew what a Protocol is capable of. Congrats!

  • @denizc1019
    @denizc1019 ปีที่แล้ว

    Concise and clear thank you, I know it is not this tutorial's subject but you need weak delegate otherwise it will cause retain cycle. Cheers!

  • @dmytronice1337
    @dmytronice1337 ปีที่แล้ว +1

    Thank you so much for this video, right now I am reading your book and it very inspiring!

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      Happy to help! Hope you enjoy the book 😀

  • @stqrsky
    @stqrsky ปีที่แล้ว

    very good explanation, i am struggling but am always motivated by you. thanks!

    • @seanallen
      @seanallen  ปีที่แล้ว

      You got this, Starsky!

  • @jaymee0523
    @jaymee0523 ปีที่แล้ว +14

    The delegate should be declared weak 😆😆😆

    • @seanallen
      @seanallen  ปีที่แล้ว +4

      Should it in this case? 🤔. The project source code is in the description. Open up the memory graph debugger and take a look.

  • @elaf7092
    @elaf7092 8 หลายเดือนก่อน +1

    very helpful

    • @seanallen
      @seanallen  8 หลายเดือนก่อน +1

      Happy to help!

  • @wasimsafdarhumma5515
    @wasimsafdarhumma5515 5 หลายเดือนก่อน +1

    Nice "Repetition is the keyword to learn this stuff"

    • @seanallen
      @seanallen  5 หลายเดือนก่อน +1

      Exactly

  • @karlostj4683
    @karlostj4683 11 หลายเดือนก่อน

    Another important idea associated with delegates and protocols: There is an implicit assumption that the "delegate" object might change. In this case, the delegate is a ViewController, but in the same app at some later point in time or some alternate context, the delegate might be some other kind of object. However, this presumes that this behavior+context is going to come up multiple times. Because if it only occurs once, setting up the protocol and managing the delegate might be a waste of time. For instance, for every UITableView, there's usually one and only one object that's going to be that table view's data source and/or table view delegate.

  • @pashaotverchenko5109
    @pashaotverchenko5109 ปีที่แล้ว +1

    Thank you for sharing that!

    • @seanallen
      @seanallen  ปีที่แล้ว

      Happy to help, Pasha!

  • @francoramiro7
    @francoramiro7 ปีที่แล้ว +1

    Super clear!🎉

    • @seanallen
      @seanallen  ปีที่แล้ว

      Thanks Franco!

  • @JakobKoblinsky
    @JakobKoblinsky หลายเดือนก่อน

    Anyone know why Apple devs call this a delegate pattern when the "this" context isn't the caller's?

  • @andriy1131
    @andriy1131 ปีที่แล้ว

    No storyboard used. I don't understand

  • @davidruvinskiy6347
    @davidruvinskiy6347 ปีที่แล้ว +18

    Your original delegates and protocols video was one of the videos that made me discover your channel. Thanks for recreating it. Keep up the good work, Sean!

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      No problem David. I appreciate the long-time support!

  • @srebulla
    @srebulla 7 หลายเดือนก่อน +8

    I've had to rewatch this video a couple of times, but now it has "clicked". Thank you for explaining this in a way so many tutorials did not!

    • @seanallen
      @seanallen  7 หลายเดือนก่อน +1

      Glad it finally clicked for ya!

  • @Stu49583
    @Stu49583 6 หลายเดือนก่อน +2

    The intern decided to hire himself 😂 I wish it worked like this in real life too 😂

  • @djryanashton
    @djryanashton ปีที่แล้ว +6

    5:50 "I'm not actually going to call from within the ViewController, I'm gonna call it from somewhere else". This was a key moment for me to understand conformance. I have previously struggled to understand delegate methods. This helped. Thanks.

    • @seanallen
      @seanallen  ปีที่แล้ว +2

      Glad it was helpful, Ryan. Delegates were tricky and took me a bit to fully grasp. I tried to use all my "light bulb moments" in this video.

  • @ashdeng4383
    @ashdeng4383 ปีที่แล้ว +4

    weak var delegate....
    otherwise retain cycle (aka. memory leak)

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      Should it in this case? 🤔. The project source code is in the description. Open up the memory graph debugger and take a look.

  • @ZulquarnainShah-u1u
    @ZulquarnainShah-u1u 6 หลายเดือนก่อน +1

    I don't really like how much code we have to write for a simple communication between views. How can we achieve this using combine framework?

  • @mediterrenean
    @mediterrenean ปีที่แล้ว +5

    Clear and concise, that is exactly what beginners need. Huge thumb-up! You don't skip over the details required to understand new concepts.

  • @YianniYiannakidi
    @YianniYiannakidi ปีที่แล้ว +5

    Dear Sean. I have bought your book, and I was inspired from your story. I want you to know that you have fans from the other side of the world 🇬🇷
    Every video you make is so helpful. Thank you for recreating in this version.

    • @numb980
      @numb980 ปีที่แล้ว

      should have bought Matt Neuburg's instead

    • @seanallen
      @seanallen  ปีที่แล้ว

      Happy to help, Ioannis 😀

  • @mfgucluer
    @mfgucluer 2 หลายเดือนก่อน +2

    The best protocol delegate video in youtube platform. Thanks a lot...

    • @seanallen
      @seanallen  2 หลายเดือนก่อน +1

      Thanks! Happy to help.

  • @kurdi-1919
    @kurdi-1919 6 หลายเดือนก่อน +1

    Amazing explanation! Really appreciate your videos.

    • @seanallen
      @seanallen  6 หลายเดือนก่อน

      Glad it was helpful!

  • @renagurbanovasenglish
    @renagurbanovasenglish 5 หลายเดือนก่อน +1

    Thank you, that was the greatest explanation ever😍😍😍

    • @seanallen
      @seanallen  5 หลายเดือนก่อน

      Glad it was helpful!

  • @mariojackson802
    @mariojackson802 ปีที่แล้ว +2

    Thank you for the analogy, Sean. I had a bit of a hard time understanding them and I just saw that you created a video about it at the right time (for me). I also bought multiple of your courses and I can only recommend them. Do you ever plan on creating an in depth course for UIKit for developers completely new to iOS development?

    • @seanallen
      @seanallen  ปีที่แล้ว +2

      Glad this was helpful, Mario and I hope you enjoy the courses! I plan on making a job preparation course later this year (for both SwiftUI and UIKit). It will replace the GitHub Followers course which is starting to get a little old.

  • @OnionKnight541
    @OnionKnight541 5 หลายเดือนก่อน

    respectfully, if you're looking for a way to teach this concept, i don't think the intern / boss metaphor is the best option.

  • @mashfikurrahman1115
    @mashfikurrahman1115 ปีที่แล้ว +2

    Really it's gonna be such a cool feature for iOS, Thanks for the excellent explanation with the perfect example! :)D

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      Glad you liked it, Mashfikur!

  • @joshuajean4683
    @joshuajean4683 8 หลายเดือนก่อน +1

    Thank you, now to create my own example to cement it. Thank you for providing source code and using time to explain analogies vs setting up UI.

    • @seanallen
      @seanallen  8 หลายเดือนก่อน

      Glad it was helpful!

  • @marinaalecriim
    @marinaalecriim ปีที่แล้ว +2

    Best tutorials! ✨

    • @seanallen
      @seanallen  ปีที่แล้ว

      Thanks Marina!

  • @Juliapak
    @Juliapak ปีที่แล้ว

    hello! I am trying to build an app with a reward card (like starbucks). Have you ever built something like this? If so is there an api/platform that lets you do this? like stripe etc???

  • @ordozgoite
    @ordozgoite 8 หลายเดือนก่อน +1

    Thank you so much!

    • @seanallen
      @seanallen  8 หลายเดือนก่อน +1

      Happy to help!

  • @JasonMitchellAZ
    @JasonMitchellAZ ปีที่แล้ว +1

    Great update to the original. You didn't want us to choose the dark side, though? (I think that's what you did last time, right?)

    • @JasonMitchellAZ
      @JasonMitchellAZ ปีที่แล้ว

      Also, I see you're not aligning your "=" anymore... when did you come over to the dark side? 😆

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      It got too tedious for my liking, lol

  • @GreenDevBabe
    @GreenDevBabe ปีที่แล้ว +1

    Thank you very much! I knew how to do it but didn’t know how to explain on interview

    • @seanallen
      @seanallen  ปีที่แล้ว

      Glad it was helpful!

  • @dan_pal
    @dan_pal 6 หลายเดือนก่อน +1

    Amazing explanation, the best Swift TH-camr imo

    • @seanallen
      @seanallen  6 หลายเดือนก่อน

      I appreciate that :)

  • @joshuajean4683
    @joshuajean4683 8 หลายเดือนก่อน +1

    Thanks for spending time explaining analogies and providing source code!

    • @seanallen
      @seanallen  8 หลายเดือนก่อน

      I appreciate the generosity! Glad you found the video helpful :)

  • @j.p.bikes22
    @j.p.bikes22 ปีที่แล้ว

    Hey Sean, great video, I am building an App in Storyboard and I put the destination.delegate = self into the button function which brings up the secound Viewcontroller. Running it showed that the protocol function never actually runs, I believe because its never actually send over. Could you help me pls?

  • @loO5r
    @loO5r ปีที่แล้ว

    That is a very good explanation for the the Delegate pattern!
    But for me the responsibility still feels twisted.
    I would pass a callback onSelected(name: String, imageName: String) into the ProductSelectionVC.
    ProductSelectionVC's responsibility would then only be to select and the ViewController has full control of what to do with this selection (in this case showing it to the user).
    In this way imho the code is also easier to understand because when the developer reads through the ViewController's code, he knows exactly what happens with the selection.
    Am I thinking wrong, do I miss something in my approach?
    Thx!

  • @leifashley
    @leifashley ปีที่แล้ว +1

    This doesn't look like swiftui... :) 👍 - good Vid though, there's a lot of UIKit code out in the corporate wild.

    • @seanallen
      @seanallen  ปีที่แล้ว

      Lol, I'm mixing in some UIKit focused content. I feel all the TH-camrs have gone all in on SwiftUI, that there is a vacancy for UIKit content.

    • @leifashley
      @leifashley ปีที่แล้ว

      @@seanallen That's a good idea. I love swiftui, but we are locked down to UIKit and programmatic at that... so work wise, this is more beneficial to me.

  • @qaziaydan1143
    @qaziaydan1143 ปีที่แล้ว +1

    You are the best ❤

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      I appreciate the kind words :)

  • @MainActor
    @MainActor ปีที่แล้ว

    For me, where this Boss and Intern analogy falls apart when I was just starting about 4-5 years ago was that the intern is creating or instantiating the boss object 😂 which is unheard of and slightly misleading at that time.

  • @Maziyarlayeghkar
    @Maziyarlayeghkar ปีที่แล้ว +1

    Finally a good video about delegate pattern. Thank you Sean!

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      Glad you enjoyed it, Maz!

  • @haydenwadsworth7064
    @haydenwadsworth7064 ปีที่แล้ว +1

    Love your videos! Always learning from you :) keep it up!

    • @seanallen
      @seanallen  ปีที่แล้ว

      Thanks Hayden!

  • @josejaimefelixgarciagarcia8884
    @josejaimefelixgarciagarcia8884 ปีที่แล้ว

    4:43, The way I think of this part is that the boss is setting up a job posting, it will create a spot (which is the var) for a person who would want to work on it (someone who sets himself as the delegate) 7:27

  • @gusgonnella7706
    @gusgonnella7706 ปีที่แล้ว

    Very helpful, thank you for making this video. As a swift novice, good videos like this are monumental in helping me understand

  • @leunamvon2565
    @leunamvon2565 5 หลายเดือนก่อน

    Very well explained, Sean.

  • @axemanaxeman
    @axemanaxeman ปีที่แล้ว

    Big fan of your original video on Delegate as I still refer it to this day. The new video on delegate is an added bonus to my swift learning path. Thank you!

  • @samr.4692
    @samr.4692 ปีที่แล้ว +1

    Great, short, and effective tutorial. Thank you!

    • @seanallen
      @seanallen  ปีที่แล้ว

      Happy to help, Sam!

  • @Maziyarlayeghkar
    @Maziyarlayeghkar ปีที่แล้ว

    Please also make one for Combine.

  • @ialtoobi
    @ialtoobi ปีที่แล้ว +1

    Thanks Sean for sharing knowledge ;)

    • @seanallen
      @seanallen  ปีที่แล้ว

      Happy to help, Muhammad!

  • @iosdev01
    @iosdev01 ปีที่แล้ว +1

    also teach how to work with table view through the protocol

    • @seanallen
      @seanallen  ปีที่แล้ว

      I have a TableView video on my channel

  • @nileshjdarji
    @nileshjdarji ปีที่แล้ว

    Out of all videos that you have made on delegates and protocols, I like this one the most. Also thanks for teaching how to do the bottom sheet as well. Thank you.

  • @t0mb0lt0n
    @t0mb0lt0n ปีที่แล้ว

    Thanks a lot Sean for simple explanation and perfect example, this example really helped me

  • @victorriurean
    @victorriurean ปีที่แล้ว +1

    nice

  • @joaquimmenezes7396
    @joaquimmenezes7396 ปีที่แล้ว

    Thanks sean !!

  • @Bertie211
    @Bertie211 ปีที่แล้ว

    I’m lost!!!

  • @srinathshah7461
    @srinathshah7461 ปีที่แล้ว

    A

  • @clearcut3674
    @clearcut3674 ปีที่แล้ว

    Thank you very much, Sean, for your hard work. Your videos are always the best! Are you planning to release joint content with Paul Hudson?

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      Glad you enjoyed it! Paul and I used to work together on a podcast, but there are no plans for joint content at the moment. There's no ill will or anything. We're just both doing our own things.

  • @krisright7304
    @krisright7304 ปีที่แล้ว

    funny, but I just went through the first protocol&delegate tutorial. And I'm wondering why you made the same video?

    • @seanallen
      @seanallen  ปีที่แล้ว +3

      Because the other video is almost 6 years old and I talk REALLY fast in it (I get a lot of complaints about that in the comments). Many people watch that video as a resource for interviews and I felt that I could make a better, more modern version of it. I'm going to be redoing my entire "Interview Tips" playlist with modern versions.

  • @_tahabebek
    @_tahabebek ปีที่แล้ว

    We would like to see the same tutorial with SwiftUI. Pretty sure it will be much cleaner and simpler!

    • @seanallen
      @seanallen  ปีที่แล้ว

      The delegate and protocol communication pattern isn't really a thing in SwiftUI. They way to communicate between views is SwiftUI is much different.

    • @_tahabebek
      @_tahabebek ปีที่แล้ว

      @@seanallen true, usually the pattern is swiftui views call functions on their viewmodels to handle user actions.