Guard or If in Swift Explained

Поділитися
Вставка
  • Опубліковано 20 гру 2024

КОМЕНТАРІ • 16

  • @GaryLutchansky
    @GaryLutchansky 2 роки тому +2

    Based on this video's title, I "knew" the content, or so I thought. Since you can always be trusted, I gave it a spin anyway. And I learned! Thank you Stewart. You're a fantastic teacher.

  • @nelsonalva
    @nelsonalva 2 роки тому

    You are not only a good programmer and teacher. You are also a great UA-camr and communicator. Keep the good work!

  • @robinandthedog
    @robinandthedog 2 роки тому +1

    I really like the simple if logic, but I see my colleagues use a lot of guards so great that we have options here, thanks for the video!

  • @obelix8477
    @obelix8477 2 роки тому +1

    Excellent! Appreciate the extra detail about setting up and using Playgrounds.
    When I first learned about guard statements, I thought of Beefeaters in front of a castle. If you're a variable, the guards will challenge you as you approach the gate. If you pass their challenge, you are granted access to the inner keep. Otherwise, hit the road!
    Also worth mentioning? If you present an optional variable, and it's unwrapped by the "guard let", the variable is available to code in the entire function. But, if the variable is unwrapped via an "if let", then the unwrapped variable is only available within the if statement's brackets. This is a great reason to lean towards using guard statements, rather than "if let".

    • @StewartLynch
      @StewartLynch  2 роки тому +1

      Yes. With if let it is only valid with the brackets.

    • @kenturnbull9679
      @kenturnbull9679 2 роки тому

      Ah True , but if you have a bottle of Gin with you you get a pass alla time

  • @KimbrellBrad
    @KimbrellBrad 2 роки тому

    Great tutorial with the comparisons! You always produce great content and it seems to always help me understand some nuance about the subject. Growing up on "IF" it has been harder for me to switch over but I have done so for the easier readability for others or even me in a year! LOL. Thanks Stewart!

    • @StewartLynch
      @StewartLynch  2 роки тому

      Glad the videos resonate with you Brad.

  • @nitesh-maharaj
    @nitesh-maharaj 2 роки тому +2

    In my early days of programming I would use a lot of Boolean algebra to deal with these types of checks. Now with years of experience behind me, my main objective is to make code more readable. Having multiple guard statements is far easier to read than a single statement containing a lot of && and/or ||. Now I'm not saying don't use Boolean algebra, I'm just saying if you feel the code is looking a bit unwieldy, you may want to refactor and simplify.

  • @mr.erikchun5863
    @mr.erikchun5863 2 роки тому +1

    Thank you sir for these high quality videos. I appreciate how in depth your explanations are.
    Have you made a video in using closures as an alternative to protocol/delegate? If not I’d appreciate if you could provide an article that explains it well. Thanks again.

    • @StewartLynch
      @StewartLynch  2 роки тому

      I haven’t as that is something I would do in UIKit, but only started creating videos when I switched pretty much entirely to SwiftUI.

  • @BSellnau
    @BSellnau Рік тому

    thanks for the great examples

  • @Mahadev-x7u
    @Mahadev-x7u 2 роки тому +1

    Special…🔥👍

  • @lazaroambrosio5646
    @lazaroambrosio5646 2 роки тому +1

    🔥🔥🔥 video

  • @danielcrompton7818
    @danielcrompton7818 9 місяців тому

    In this first example I'd have returned `person.age > 20 && person.gender == .female && person.skills.contains(.swiftui)`