Clean Swift Code with Where Clause

Поделиться
HTML-код
  • Опубликовано: 4 июл 2024
  • Go to squarespace.com/seanallen to save 10% off your first purchase of a website or domain using code SEANALLEN.
    I love the where keyword in Swift. It cleans ups your code and makes it read much nicer. In this video I'll show you a few common examples of using the where keyword including for loops, finding elements in an array, and protocol extension.
    My iOS Dev Courses:
    seanallen.teachable.com/
    My Source Code:
    seanallen.teachable.com/p/sou...
    X (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):
    Mark Moeyken’s SwiftUI Books:
    www.bigmountainstudio.com/a/f...
    Paul Hudson's Hacking With Swift:
    gumroad.com/a/762098803
    RocketSim - Enhance Your Xcode Simulator:
    gumroad.com/a/51797971/ftvbh
    Objc.io Books (Thinking in SwiftUI & Advanced Swift):
    gumroad.com/a/656585843
    Timestamps:
    0:00 - Where Keyword - For Loops
    1:29 - First Where
    3:04 - Contains Where
    4:05 - Protocol Extension Constraint
    #swift #softwaredeveloper #iosdeveloper
  • НаукаНаука

Комментарии • 32

  • @seanallen
    @seanallen  Месяц назад +3

    Learn more Swift, SwiftUI and UIKit with my iOS Dev courses at seanallen.teachable.com

  • @michaelforrest
    @michaelforrest Месяц назад +9

    I didn’t know about the for:in:where thing but two of those weren’t using the where keyword at all- they were just function parameter names!

    • @NicoTasche
      @NicoTasche Месяц назад +3

      Just wanted to give the same comment, those array functions first, just use their external parameter names, nothing todo with the keyword.

    • @seanallen
      @seanallen  Месяц назад +3

      Fair point.

    • @JOyo246642
      @JOyo246642 Месяц назад

      Yep came here to comment as well. You can see it clearly by the different syntax highlighting.
      The other where usage that I think is worth sharing is inside of a switch.

  • @arjundureja
    @arjundureja Месяц назад +2

    Using this on extensions is so nice. I love Swift's type safety

  • @YEIYEAH10
    @YEIYEAH10 29 дней назад

    Fantastic video. “Where” in the array extension was something that I wasn’t fully aware of. Thank you so much.

  • @rdr9999
    @rdr9999 Месяц назад

    I really appreciate your teaching style, Sean. This is great stuff, I always learn something useful. My interest is primarily targeting macOS with SwiftUI. Are your courses applicable to that, or more strictly for iOS?

  • @boxbrain
    @boxbrain Месяц назад

    Thanks Sean Allen! Such a smooth transition towards the end too 🤭

  • @JasonMitchellAZ
    @JasonMitchellAZ Месяц назад +3

    Thanks for the quick video reminder!

  • @irmccausland
    @irmccausland Месяц назад

    I’m new to swift so I’m just trying to clarify my understanding but you mentioned the where usage in first and contains methods are using the where “keyword” but aren’t those usages technically argument labels that are labeled “where” out of convention?
    Great videos. Quick and to the point, really helping me write more idiomatic swift. 🎉

  • @femialiu
    @femialiu Месяц назад +5

    5:22 😂😂

  • @ianfrye8988
    @ianfrye8988 Месяц назад +2

    Great video as always!

  • @petersuvara
    @petersuvara Месяц назад

    This is excellent. I didn’t know that you could use where in a for loop! Always used it for protocols.

    • @seanallen
      @seanallen  Месяц назад

      A little less nesting in a for loop is always a good thing :)

  • @OnionKnight541
    @OnionKnight541 Месяц назад +7

    is this a new keyword? why haven't i ever seen this ???

    • @seanallen
      @seanallen  Месяц назад +4

      Pretty sure it's been around since the early days of Swift.

    • @30guarino
      @30guarino Месяц назад +13

      'where' have you been?

    • @denisblack9897
      @denisblack9897 Месяц назад

      8 years of earning money with Swift, never heard of it too😅
      It’s like algorithms, cool but no real benefit

  • @abdallahismail-nh2ot
    @abdallahismail-nh2ot Месяц назад

    keep the good work up you are the best ❤

    • @seanallen
      @seanallen  Месяц назад +1

      Thank you! Will do!

  • @arovdev
    @arovdev 23 дня назад

    Hello, do you know something about MVVM Clean Architecture, there are three layers Presentation, Domain, Data, if so, could you make a video about it?

  • @ibringit
    @ibringit 16 дней назад

    Hi brother I have problem publishing my swiftui can you please help

  • @byhabiboff
    @byhabiboff Месяц назад +1

    😍

  • @Patrick462
    @Patrick462 25 дней назад

    Me, an old man yelling at clouds: But note that when you start using "where" in your code, you're requiring ALL FUTURE READERS OF YOUR CODE to know the "where" keyword.
    Is that good? bad? neither? irrelevant? I dunno, but you should make that decision explicitly.

  • @koscarico
    @koscarico Месяц назад

    Sometimes i feel like new logic and syntax makes you have to test it to make shure it does what your regular if statements do. And then it just feels a little dumb

  • @victorriurean
    @victorriurean Месяц назад +1

    👋

  • @kdtechniquesofficial6153
    @kdtechniquesofficial6153 Месяц назад

    Or 😉
    extension Array where Self == [Int] {
    func sum() -> Int {
    reduce(0, +)
    }
    }
    Or 😜
    extension Array where Self == [Int] {
    func sum() -> Self.ArrayLiteralElement {
    reduce(0, +)
    }
    }

    • @JOyo246642
      @JOyo246642 Месяц назад +1

      or
      extension Array {
      func sum() -> Int {
      reduce(0, +)
      }
      }