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 - Наука та технологія
Learn more Swift, SwiftUI and UIKit with my iOS Dev courses at seanallen.teachable.com
Using this on extensions is so nice. I love Swift's type safety
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!
Just wanted to give the same comment, those array functions first, just use their external parameter names, nothing todo with the keyword.
Fair point.
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.
Fantastic video. “Where” in the array extension was something that I wasn’t fully aware of. Thank you so much.
Thanks for the quick video reminder!
No problem!
Thanks Sean Allen! Such a smooth transition towards the end too 🤭
Great video as always!
I appreciate that
This is excellent. I didn’t know that you could use where in a for loop! Always used it for protocols.
A little less nesting in a for loop is always a good thing :)
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?
keep the good work up you are the best ❤
Thank you! Will do!
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. 🎉
is this a new keyword? why haven't i ever seen this ???
Pretty sure it's been around since the early days of Swift.
'where' have you been?
8 years of earning money with Swift, never heard of it too😅
It’s like algorithms, cool but no real benefit
5: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?
Hi brother I have problem publishing my swiftui can you please help
😍
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
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.
👋
Or 😉
extension Array where Self == [Int] {
func sum() -> Int {
reduce(0, +)
}
}
Or 😜
extension Array where Self == [Int] {
func sum() -> Self.ArrayLiteralElement {
reduce(0, +)
}
}
or
extension Array {
func sum() -> Int {
reduce(0, +)
}
}