Swift Closures Explained

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 พ.ย. 2024

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

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

    My iOS Developer Courses - seanallen.teachable.com

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

    I’m going to be honest with you, this was a G-R-E-A-T explanation. I’ve read so many Medium posts and Stack Overflow answers where people either try to sound too smart or they just don’t realize that they’re not speaking in layman’s terms (they’re speaking to other people who don’t need it explained). This took a very simple approach to something complicated and made it simple. FANTASTIC explanation!!! You do a great job of keeping things simple! Seriously. And I like your graphics!

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

      Wow, thanks for the kind words, Lance. I'm happy to hear you appreciated the effort I put into the graphics and simplifying my explanation 😀

  • @evan_ry
    @evan_ry ปีที่แล้ว +7

    That 15 minutes just saved me so much time developing my first native macOS app.
    Thank you!!
    Best Swift tutorials on TH-cam.

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

      Thanks for the kind words, Evan!

  • @王登科-u5c
    @王登科-u5c ปีที่แล้ว +4

    I absolutely loved the Swift Closures Explained video by Sean Allen. As someone who has struggled to understand closures, this video was a godsend. Sean does an amazing job of breaking down the confusing aspects of closures, like what the parenthesis mean and the shorthand syntax. He explains everything in a clear and concise manner, making it easy for even a beginner like myself to follow along.
    I also appreciate the fact that Sean provides a promo code for Squarespace at the beginning of the video. It’s always great to get a discount when you’re starting a new website or domain.
    Overall, I highly recommend this video to anyone who is struggling to understand closures, or anyone who wants to brush up on their Swift knowledge. Thank you, Sean, for creating such an informative and helpful video.

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

    WOW! The most Clear and concise explanation I've seen on closures in 6years of developing.

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

    (well)->(explained) {Thanks a lot for great video}

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

      I see what you did there...

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

      @@seanallen @Vadiraj Hippargi actually its (explained) -> (well). Your get your explanation and return an answer "well"

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

    my issue with this video is it isn’t a discussion of closures in swift but a discussion of a syntactic sugar for function pointers.
    unless i missed something, there wasn’t any point where you “closed over” or captured the environment when creating the closure, which is an essential part of closures. i assume you can also do this in swift closures but it wasn’t shown here

  • @Adrian.katzenstein
    @Adrian.katzenstein ปีที่แล้ว +3

    Hi Sean! I've been working on my programing career for about 2 months now. I'm doing the 100 days of swift with Paul Hudson. Even tho this is all still a bit blurry i do appreciate your content since its starting to clear a bit of the fog that i had. Just wanted to stop by and say thanks! You'll probably see me around a bit more ✌

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

      Happy to help. Welcome to the channel!

  • @miguel.sanchez
    @miguel.sanchez 4 หลายเดือนก่อน +2

    It's weird, but when you explained how VStacks use trailing closure syntax, my understanding of swiftUI drastically improved. I am the type of person that not only wants to memorize the syntax, but also understand why it is written like that. Thanks!

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

      Happy to hear it finally clicked :)

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

    best teacher i found about swift fundamentals

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

    Thank you so much for clearly explaining the $0 and $1 in closures. I have read and studied it so many times but now I just got it in 1 minute.
    Thank you!

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

      I wanted to be sure to explain that because the $0 and $1 tripped me up for a LONG time. Glad I could help!

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

    Before I start this video, I started searching for videos on closures , while actively using “100 days of SwiftUI” lesson plan, but during the first couple lessons involving closures, I could not get out of my head the question “If functions do everything closures do, why then do closures even exist?” So hopefully I get that answer from this video, because this thought it slowing down my ability to progress in learning swift.

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

      My brother! Wow, I am also doing 100 days of Swift with Paul Hudson and I got confused by this as well. 😂 hope you pushed through 💪🏾 I just got here

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

    I am currently trying to learn Swift. And with every bit I code and video I watch I learn a 1% crumb of that language. But with the trailing closures explained it felt like an iceberg of 10% crashed into the water. Thank you.
    I think, maybe Swift is so hard to learn, because in all tutorials you learn the shortcut syntax first and never get explained why this actually works. But it would be better to understand the full syntax and all the principals first and then teach the shortcuts.

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

    I've just started delving into the mystery that is closures. Your video helped both for what they are and, especially, why I will need to know them. Thanks!

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

      Glad it was helpful!

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

    Was confused AF going through swift tour, but this video really cleared everything up.. thanks!!

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

      Glad it helped!

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

    This video came at Great time , currently figuring out this developing game.🙏🏾

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

      Happy to help, Kerrion! Closures were tough for me to figure out when I was just learning.

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

      My thought exactly!

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

    Finally my Javascript knowledge helps me out. I picked these up quickly.

  • @biganski
    @biganski 9 หลายเดือนก่อน +1

    I should have known that I could trust another Sean to explain this well

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

      Haha, glad you liked it.

  • @0x0mnihaze
    @0x0mnihaze 2 ปีที่แล้ว +7

    Simple, nice and clear. Thanks for the video, Sean!

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

      Glad it was helpful!

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

    Sean you are without a doubt my number 1 iOS teacher. Thank you.

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

      I appreciate the kind words :)

  • @pranavsubash689
    @pranavsubash689 9 หลายเดือนก่อน +1

    Extremely useful video. I came here for closures and learnt so many things I was confused ab all in one go thanks

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

      Glad it was helpful!

    • @pranavsubash689
      @pranavsubash689 9 หลายเดือนก่อน +1

      @@seanallen Oh wow I can’t believe you replied. I rly like the way you teach by the way - I just reached ur 3rd video in the visionOS series. Exciting times we live in

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

      Hope you enjoy the visionOS content. Much more to come!

  • @david-holmes
    @david-holmes 2 ปีที่แล้ว +8

    Thanks, that cleared up some things I’ve been putting off for a long time 👍🏻

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

      Thanks again for the generous support, David 😀. I put off REALLY learning closures for a long time too... I was very intimidated by them early in my career.

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

    8:30 sweet. Gave a special style to the video.

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

      A lot of times when I’m editing the video I’ll think of ways I could have explained things more clearly. That’s what that was :)

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

    closures are my favourite part of Swift

  • @dixztube
    @dixztube 10 หลายเดือนก่อน +1

    Not a swift dev but some good information here that if you already know this it makes it connect from a different perspective. Great video

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

      Thanks for the kind words!

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

    I’m on my phone so not sure if this has been said before but it took me a while to figure out how to sort on multiple values….
    Tuples!!!!
    { ($0.score, $0.name) > ($1.score, $1.name) }
    This will sort first by descending score, then by descending name.
    To sort by DESC score and ASC name:
    { ($0.score, $1.name) > ($1.score, $0.name) }

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

    Awesome video, Sean! Thank you for all the work you do!

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

      Thanks Tyler. Glad you enjoyed it!

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

    Hey, very interesting!
    Just needed some advice on when should I use a function or computed property with closures?

  • @VideoTubeFan
    @VideoTubeFan 3 หลายเดือนก่อน +1

    Great explanation. And setting the speed to 0.75 helped a lot 😉

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

      Thanks!

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

    Where did the .filter come from? Is it part of the standard library (foundations) along with append, etc.?

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

      Yes. .filter and .sorted are part of the standard library for Swift.

  • @mostly_def
    @mostly_def 9 หลายเดือนก่อน +1

    Excellent content! This answered so many questions quickly and kindly 👏

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

      Glad it was helpful!

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

    Wow, Sean. You are a very good teacher. Thank you for this. 👍🏼

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

      Glad you enjoyed it, Tom 😀

  • @李淳澔
    @李淳澔 ปีที่แล้ว

    What a wonderful tutorial, clear but also full of knowledge. You really helped me understand the book of swiftUI, thanks a lot !

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

    "empty parentheses and Void are the same thing" - man oh man, this is the kind of stuff that takes hours off the head banging waste of time trying to learn

    • @GabeColors
      @GabeColors 2 ปีที่แล้ว

      I've been looking for long lists of "this is the exact same result but a different way to get there in Swift" for 3 years

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

      at 2:55 it would be awesome to keep that "other" code visible, comment it out, then we could see and compare the other way.

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

      Thanks for the feedback, Gabe. I'll keep that in mind for future videos. Glad you enjoyed this one!

  • @azackmatoff2570
    @azackmatoff2570 25 วันที่ผ่านมา

    Is this to manage memory better? You guys, swift, have Class and Struct, Struct goes under Stack whereas Class goes to Heap, as well as functions. So, that's why UI uses Structs (correct me if I'm wrong, I'm a flutter dev, not a swift), is the reason you guys have closures? BTW, we don't have them. I mean, you mentioned closures are being used in UI. Of course all of these are done for a better performance(?)

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

    A Question, is the var topStudentFilter always recalculated when 1. Its called, or 2.when the student array is changed, or3. at both times.

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

    Hey Sean, thanks for this. Can you make a video about Callbacks? Differences between completion handlers and Callbacks?

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

    Looking for ways to identify closures when we see their address in memory graph during debugging.
    So often they just show an address. Is it possible to make a trailing closure more recognizable in mem graph? Or to find a closure by address wile debugging? Or to print the address of a trailing closure?

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

      Not that I'm aware of

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

    Explained very well.

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

      Thanks!

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

    Great content , is this the same to SwiftUI ?

  • @mykalimba
    @mykalimba 10 ชั่วโมงที่ผ่านมา

    8:00 I'm just starting to learn Swift, and the more I use it and Xcode, the more "WTF, Apple?!" moments I have. Like here: why does their sort function expect a boolean return value instead of the typical tertiary value (less than 0, 0, greater than 0)? How does a Swift sort behave predictably with only a boolean return value?

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

    Thanks for the great videos, closures have been a source of mystery for me since I started. One question, in the first example, is the closure being passed in as a type for topStudentFilter?

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

    Excellent examples! Now do the same example with Async Await

  • @afixithei.V
    @afixithei.V 2 ปีที่แล้ว

    Should I invest my time in Swift UI or UIkit? I'm an experienced Android developer and just starting to learning iOS development to boost my portfolio; not necessarily trying to get a job as an iOS dev at least for the next 1 - 2 years

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

      I would recommend you ask this type of questions on Reddit. There's a very active sub for iOS devs. Personally I would start with UIKit since the majority of companies still use it, and it's easier to go from UIKit to SwiftUI than the other way around.

    • @Spacer-l3j
      @Spacer-l3j 8 หลายเดือนก่อน

      @@hansalucas yeah i can confirm... start with UIKit and then SwiftUI. The other way around will feel like a headache

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

    Well done, great explanations 👍

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

      Glad you liked it, Stuart!

  • @farhadali6956
    @farhadali6956 2 ปีที่แล้ว

    Code magician, expecting more :) with closure and SwiftUI

  • @ЛеонидОлеговичШевченко
    @ЛеонидОлеговичШевченко 2 ปีที่แล้ว +1

    Super understandable! Thank you for this video, finally i've got it 🤝

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

      Happy to hear that!

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

    Can I see a closure as an interface like in Java

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

    Hi Sean are closures like object in java where you can call them whenever you need them

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

      Not exactly like an object. Closures are functions, but without the function declaration.
      Some variables are declared to be Int. Some are Strings. Others are Student, or some other custom struct.
      But, variables can also be “ functions that take certain types as parameters and return a certain type”
      So you can say:
      var aVar: (Student, Int) -> [Student]

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

    Another good one.

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

      Thanks Glenn!

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

    really nice thanks

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

      No problem, Gregory

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

    I am a fan but I think Dr Angela Yu’s course explain it a little better this time.

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

    Chuckled at the escaping one 🤣

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

      Just a small glimpse into my inner-monologue while coding.

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

    Suggestion: When teaching to beginners, use unique variable names throughout your code and try not to repeat them between functions that don't end up passing the variable out of it anyways… @6:25 you talk about how we could have used 'persons' instead of 'student' but are using 'student' because it "makes more sense, they're students"… but I see the name 'student' being used at least 13 times on that one screen alone. As stupid as it sounds when I'm learning the new syntaxes I type in variable names like "sparkleUnicorn" and "bubaTheBillyGoat" (silly name placeholders) to keep track of where the variables actually point and go to. When the tutorial overloads with one word being re-used over and over again it just makes my head spin.

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

      I'm sorry about your head. Maybe computer programming isn't for you.
      As far as overuse of the word "student" is concerned, it's only seldom repeated assigned as a variable. Mostly student is written with an uppercase S. In these cases, Student is used to refer to the initialiser for instances of the struct Student. To create an array containing instances of Student, the word Student has to be used each time for each instance in order to initialise it to that type, Student, and none other - not UIView, nor UnsafeNonblockingEquitable, nor SparkleUnicorn for that matter. Swift uses an unenforced convention of capitalising the first letter of struct and class names, lowercase for everything else, making the potential for any ambiguity rather less dizzying.

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

      @togidubnus that snide remark was really unnecessary. Your explanation about why `student` appears so often was useful. It’s just a shame the way you started your comment.

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

    “@escaping ? …. Who are we running from?” 😂

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

    So now think of escaping as you’re putting popcorn in the microwave. But it’s taking so long to pop, you ask that one friend who’s in the kitchen to wait for it to finish popping and bring it back to the living room?

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

      Lol, that's certainly one way to remember it 😀

  •  ปีที่แล้ว

    Thank you for your explanation. Coming from typescript the curly brackets kinda confused me alot. This video really explained well how SwiftUI are just bunch of shorthand ways of writing trailing closures. Could you explain the following code for me? I know what it does, but how did this shorthand version got formed? What would it's "full hand" counterpart be?
    ```
    Button {
    showDetail.toggle()
    } label: {
    Label("Graph", systemImage: "chevron.right.circle")
    .labelStyle(.iconOnly)
    }
    ```

    •  ปีที่แล้ว

      I think I figured it out, and that a function can have multiple trailing closures.

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

    very good video!

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

      Glad you liked it!

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

    anyone else absolutely hate some of this syntax?

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

    Wry little of this has to do with or rely on closures…..
    Anonymous functions, yes.

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

    awesome

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

      Glad you liked it 👍

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

    Perfection

  • @raul286162
    @raul286162 10 หลายเดือนก่อน +1

    As a flutter developer who is learning SwiftUI I must say that it is the most horrible syntax I have seen for such a simple mechanism known in many other languages ​​such as "Callbacks".

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

    slow down plz

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

      Change your video play speed…

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

    The video is great! But his definition of a a closure isn't correct. He says that closures are functions that can be passed around. But all functions can be passed around. The real difference between a closure and a function is that a closure has access to variables in the parent scope without needing to pass them in as arguments. This is a Square-Rectangle relationship. All closures are functions but not all functions are closures. What this video shows is that Swift has a short syntax for defining closures that makes them easy to use functions. But the shorthand syntax isn't what makes it a closure. Non-closure functions don't have access to variables in the parent scope unless you pass them in as arguments. Closures can just access variables in the parent scope without having to pass them in as arguments. The examples in this video are good for creating functions with a shorthand syntax that match the function signature but the examples in this video aren't unique to closures.
    In Swift all functions are closures. If you define a function (not with closure syntax) in Swift inside of a scope it will have access to variables in the parent scope. This makes it a closure. But this isn't true in all languages and is an implementation detail of the language.

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

    You are speaking much too fast. A learning viewer needs pace to process your information.

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

    Most easiest concept in swift is closure

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

    I love this!

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

    Thanks!

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

      Thanks for the generous support! Hopefully this cleared up Closures for ya 👍