Clean Swift Code with Where Clause

Поділитися
Вставка
  • Опубліковано 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

  • @arjundureja
    @arjundureja Місяць тому +2

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

  • @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.

  • @YEIYEAH10
    @YEIYEAH10 29 днів тому

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

  • @JasonMitchellAZ
    @JasonMitchellAZ Місяць тому +3

    Thanks for the quick video reminder!

  • @boxbrain
    @boxbrain Місяць тому

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

  • @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 :)

  • @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?

  • @abdallahismail-nh2ot
    @abdallahismail-nh2ot Місяць тому

    keep the good work up you are the best ❤

    • @seanallen
      @seanallen  Місяць тому +1

      Thank you! Will do!

  • @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. 🎉

  • @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

  • @femialiu
    @femialiu Місяць тому +5

    5:22 😂😂

  • @arovdev
    @arovdev 22 дні тому

    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 15 днів тому

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

  • @byhabiboff
    @byhabiboff Місяць тому +1

    😍

  • @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

  • @Patrick462
    @Patrick462 24 дні тому

    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.

  • @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, +)
      }
      }