6:54 "You probably don't want to name your functions as long as this." I would disagree with that to an extent. You can either have a vague function name and comments, which will more than likely not get updated if someone changes the code. Or you can have a longer, but more meaningful function name that actually explains what is happening. A good example of this is a guy I used to work with. He hates how comments always become out of date and doesn't use them too much. So instead, he uses longer, but more meaningful function names. We once made fun of him because he had a function name that was close to twice as long as filterWithPredicateCondition(). But on the flip side, everyone who looked at the function knew it's exact purpose.
I am starting university in a couple weeks and by previously watching ur top quality videos on software engineering u have really inspired me to take this course even more because I now realise the many things you can do as a software engineer especially creating apps and how much you can potentially earn thanks👌🏾
Hi Brian, great video. However I think you should have mentioned trailing closures too, as it simplifies the code quite a bit. func greaterThanPredicateClosure(numbers: [Int], closure: (Int) -> Bool) -> [Int]{ // body } let result = greaterThanPredicateClosure([1, 2, 3, 4, 5], { value in return value > 5 }) Here, we use the weird closure as parameter syntax. let result = greaterThanPredicateClosure([1, 2, 3, 4, 5]){ value in return value > 5 } Here, we use the trailing closure syntax, where if a closure is the last parameter of a function, it can be written outside the function parenthesis, making it easier to read. Other than that, I liked the video.
Yes, I didn't mention that because it'd be confusing for beginners. I believe only the first two parameters get values like that ($0 and $1) or does it extend to the number of parameters the closure has ($0...$n)?
Avi Sekhon Ahhh. I am a professional programmer but am just trying to learn Swift and iOS development for the first time. I was trying to fill out an autocompleted method call and XCode kept switching it to this format and I was so confused. That is really some weird syntax for someone coming from JS, Java, C#, etc.
Kind of syntax used in Parse and firebase for querying I guess, also using clousures for completion handlers but with no returned values because they are async.
I need to load the chat data for tableview. I have already used fetched results controller for getting the data . 1. Let me just explain how can I load bunch of data while scrolling to top 2. While I come friend list, how much data can I load to chat page on first time
I was reading the closures section in the Swift Guide in Apple Books, didn’t search anything about it but suddenly this video popped up in recommendations. I hope it is just coincidence
Hello. Interesting video. I have never used closures like that. Mostly for async tasks so far. Will try to integrate a few of your examples in my future projects. Cheers.
When I enter this into playground and do what you did press the run button I only get the response ready at the top and no results, this is exactly how I run ios apps is there something different going on in playgrounds?
Hey Brian, I follow your videos to make my code robust and reach to standard. Can you please make a video to clarify the difference between Array and Set.
Hi Brian, I think it would be also nice to say about short-way syntax of closures. For example: func closureTest(closure: (Int) -> Bool, array: [Int]) -> [Int]{ return array.filter(closure) } let result1 = closureTest(closure: {(num) -> Bool in return num > 4 }, array: [1,4,5,6,7,8]) let result = closureTest(closure: {$0 > 4}, array: [1,4,5,6,7,8]) I think it's obvious, that short-way syntax much more comfortable for using in code.
Hi. Thanks Brian and Apple for creating closures. It's less typing when you need to modify some logic. The closure within a closure is like a dream within a dream within a dream- Inception movie:-) God bless Proverbs 31
If the array is left as [10,5,1,2,0] you get from the modulus [10,5,0] rather than the expected [10,5]. Can you explain that and what to do to weed it out? I understand the modulus but how to return it without the zero? Thanks
As I said, I understand the modulus. Any value divided into zero produces zero which makes the value true so it adds it to the array. I'm asking how to produce [10,5] and avoid the [10,5,0] result. Cheers.
within closures, i see lots of time people use [weak self] to prevent retain cycle, i mean how do we figure out if inside a closure there is a retain cycle ? can you explain more about it in you future tutorial ? thanks very much
Great video Brian as always! Thank you for videos they are really awesome! At the moment I am very curious about how to handle uploading multiple images. I think this will be a great topic. Please let me know your thoughts!
Does Swift have anything similar to "Promises" or "async/await" in Javascript? And would it be possible to get a tutorial on the "dispatch" function in the future?
Love your videos! I'm currently learning Swift but lately have been hearing a lot about Progressive Web Applications and honestly a it does scare beginners like me learning IOS programming when people keep saying this might replace it. Can you do a video on it? I'm sure you've been hearing it a lot lately
I've been learning swift for about 3 months now and lately it's just been popping up on my youtube feed. It just gets a bit scary while you're learning native apps and these things keep popping up saying it'll replace native apps...
Trying to identify key components of closures: (a) the struct/func/etc. that defines the closure signature will also be the caller the closure; (b) the closure definer (or where the closure body is implemented) is where the exact VALUES for the parameters are located. This guy MUST match the defined signature, but he gets to define the implementation of the closure against the values in his scope.
Hey Brian! Thanks for the video. I am new to Swift and the concept of closures and I have a question. Wouldn't it be more sensible to create an extension on Array for this purpose? Something like: extension Array { func filterList(_ isIncluded: (Element) -> Bool) -> [Element] { var result = [Element]() for x in self where isIncluded(x) { result.append(x) } return result } } So you could call it via dot notation on your Array: let filteredList = [1, 2, 3, 4, 5, 6, 7, 8].filterList { $0 > 3 } Thanks in advance!
As you might or might not know already, Swift already comes with the filter function out of the box for arrays. The idea of closures applies to a much broader and generic programming paradigm. Today's lesson was simply an introduction on how to write out the syntax.
Lets Build That App Thank you very much! As I'm pretty new to programming, with Swift being my first programming language (actually studying life sciences), I'm learning tons of new stuff every day and your videos help me out a lot.
FINALLY someone who made me understand closures, thank you!
totally agree - was completely lost until I saw this vid.
Eventually someone decided to make closure understandable and easy to use thank you sir !
Now I seem to be getting pretty much closer to the point of understanding WHY do we need closures)) Thanks a lot!
Best tutorial for Closures so far. Thank you man.
Bro... you save me from understanding closure! Thanks a lot!
I just tested this code:
func filterWithPredicateClosure(closure: (Int) -> Bool, numbers: [Int]) -> [Int]{
let greater = numbers.filter(closure)
return greater
}
filterWithPredicateClosure(closure: {
return $0 > 6
}, numbers: [1,2,3,4,5,10])
It successfully run. :D Thanks Brian.
6:54 "You probably don't want to name your functions as long as this." I would disagree with that to an extent. You can either have a vague function name and comments, which will more than likely not get updated if someone changes the code. Or you can have a longer, but more meaningful function name that actually explains what is happening. A good example of this is a guy I used to work with. He hates how comments always become out of date and doesn't use them too much. So instead, he uses longer, but more meaningful function names. We once made fun of him because he had a function name that was close to twice as long as filterWithPredicateCondition(). But on the flip side, everyone who looked at the function knew it's exact purpose.
True, I'm also in the camp for longer function names as long as it makes grammatical sense.
Agree, self documenting code is better than commenting document/explanation.
One of my favorite youtubers! Awesome stuff!
I am starting university in a couple weeks and by previously watching ur top quality videos on software engineering u have really inspired me to take this course even more because I now realise the many things you can do as a software engineer especially creating apps and how much you can potentially earn thanks👌🏾
I love these kind of videos, where you explore the code syntax for advanced stuff. Nice and clean, thank you brian!
Best tutorial on closures !!!! I want to learn more about @escaping closures.
You are a great teacher. Thank you
good explanation thanks. why are they use some times escaping key word and why we are put front of the parameter _ these symbol
like _escaping
THE NEW TH-cam UPDATE IS AMAZING!!!!
Hunter Gray are you using phone or pc
Yet another pointless changes I need to learn.
I agree outstanding material design layout! finally they used it
If you are appending the filtered values to the array, do you need to clear out the array every time you call the function?
Thank you very much for making this tutorial video and, for making it with simple explanations so I can understand it. I love it !!
Hi Brian, great video!
how about a follow up with closures that contain important keywords like @escaping in Swift?
please
nice work with the audio, real pro level stuff.
Hi Brian, great video. However I think you should have mentioned trailing closures too, as it simplifies the code quite a bit.
func greaterThanPredicateClosure(numbers: [Int], closure: (Int) -> Bool) -> [Int]{
// body
}
let result = greaterThanPredicateClosure([1, 2, 3, 4, 5], {
value in
return value > 5
})
Here, we use the weird closure as parameter syntax.
let result = greaterThanPredicateClosure([1, 2, 3, 4, 5]){
value in
return value > 5
}
Here, we use the trailing closure syntax, where if a closure is the last parameter of a function, it can be written outside the function parenthesis, making it easier to read.
Other than that, I liked the video.
you can even reduce it to {$0>5}
Yes, I didn't mention that because it'd be confusing for beginners. I believe only the first two parameters get values like that ($0 and $1) or does it extend to the number of parameters the closure has ($0...$n)?
i believe it does expand to $n
Avi Sekhon Ahhh. I am a professional programmer but am just trying to learn Swift and iOS development for the first time. I was trying to fill out an autocompleted method call and XCode kept switching it to this format and I was so confused. That is really some weird syntax for someone coming from JS, Java, C#, etc.
I really don't think that's easier to read ; I truly believe that's far more confusing to read
Beautiful closure explanation..!! Thank you!
Really needed this! Many thanks! You have yourself a new subscriber.
Kind of syntax used in Parse and firebase for querying I guess, also using clousures for completion handlers but with no returned values because they are async.
+Misael Garay yes this is pretty much how the parse and firebase completion handlers are written. Hope you can use this knowledge in your own apps
Another great video, everyday you surprise me with what I can learn, thanks !!
Nice explanation , I've been watching tutorials about this in the last days , I thinks that is a very useful concept
Nicely explained 🤩, thank you
Hi Brian, really need a lesson of weak self and strong self in swift and how to avoid the memory leak in closures. Many thanks!
Nice but still I have a doubts when we can use function and when we can use closure and completion handlers . Y we have to use those
Thanks paused the video and used a closure to solve some mases in that ios swift game. Was a lot of fun.
Brilliant. Really good explanation of closures.
Is my internet slow or the video is blurry? Tried in different browsers
Great video. Keep it up Brian ✌🏼
I need to load the chat data for tableview. I have already used fetched results controller for getting the data .
1. Let me just explain how can I load bunch of data while scrolling to top
2. While I come friend list, how much data can I load to chat page on first time
I was reading the closures section in the Swift Guide in Apple Books, didn’t search anything about it but suddenly this video popped up in recommendations. I hope it is just coincidence
Hello. Interesting video. I have never used closures like that. Mostly for async tasks so far. Will try to integrate a few of your examples in my future projects. Cheers.
Thanks man. Very helpful
This was wonderful explanation. I’ll use my code with closure an upcoming days 🙂🙂
Your videos are so helpful. Very clear and informative. Thank you!!!
Hi brian! Nice explanation of closures! It will be nice to see the `Generics` tutorial. Thanks
Totally agree, a tutorial on generics would be wonderful !
+Qiao Hu I believe the lessons on stacks and generics shows you how to use em
When I enter this into playground and do what you did press the run button I only get the response ready at the top and no results, this is exactly how I run ios apps is there something different going on in playgrounds?
Never mind see it
closures complicate very much for few advantages is it right?
finally understood closures:)
Amazing! Big thanks!
How do you make selected codes to //comment?? What is the shortcut?
Sir its awesome thanks for your contribution for making our future bright...
Why do you have to end the variable "filteredSetOfNumbers" with () ?
This is how we use closures.
Nicely Explained
too much thanks for you'r hard work
This video is so helpful. Thanks for posting it!
Hey Brian, I follow your videos to make my code robust and reach to standard. Can you please make a video to clarify the difference between Array and Set.
A set is a collection of items without ordering, whereas an array is sequenced.
Hi Brian, I think it would be also nice to say about short-way syntax of closures.
For example:
func closureTest(closure: (Int) -> Bool, array: [Int]) -> [Int]{
return array.filter(closure)
}
let result1 = closureTest(closure: {(num) -> Bool in
return num > 4
}, array: [1,4,5,6,7,8])
let result = closureTest(closure: {$0 > 4}, array: [1,4,5,6,7,8])
I think it's obvious, that short-way syntax much more comfortable for using in code.
I don't like the short-hand method since it seems a bit lazy and harder to read. It's a nice idea though
Hi. Thanks Brian and Apple for creating closures. It's less typing when you need to modify some logic. The closure within a closure is like a dream within a dream within a dream- Inception movie:-) God bless Proverbs 31
Very clear and informative, thank you! 😇
If the array is left as [10,5,1,2,0] you get from the modulus [10,5,0] rather than the expected [10,5]. Can you explain that and what to do to weed it out? I understand the modulus but how to return it without the zero? Thanks
0 is divisible by 5 no?
As I said, I understand the modulus. Any value divided into zero produces zero which makes the value true so it adds it to the array. I'm asking how to produce [10,5] and avoid the [10,5,0] result. Cheers.
Would I just test if value != 0 but where to put it? How to skip 0 or how to ignore it?
+waltersumofan yeah your filter function would check for AND not equal zero
Thanks
One of the best videos of using closures in a simple way.Great One Thanks.But do you know any source for going in depth.
6:02 There is no Editor -> Refactor -> Rename ... in playgrounds?
How come playground didn't ask for a value parameter when he implemented it as a closure.... It did for mine...
within closures, i see lots of time people use [weak self] to prevent retain cycle, i mean how do we figure out if inside a closure there is a retain cycle ? can you explain more about it in you future tutorial ? thanks very much
Love your videos! Been helping me so so much. I don't like how you panned the audio though (: Still thumbs up for being so awesome
Another really useful tutorial! Thanks a lot
Great tutorial, thank you Brian
Great video Brian as always! Thank you for videos they are really awesome! At the moment I am very curious about how to handle uploading multiple images. I think this will be a great topic. Please let me know your thoughts!
You’re the man brother
Hi... Brian.... I have a problem how I code for universal application.. because i facing a size problem with ipad... Sorry about my English
use size classes
NK mewara
use auto layout constraints & set your target for Universal instead of iPhone or IPad
I don't want to use auto layout constraints... I just want pure code... Like @brain
Any tutorial about size classes.. I'm quite new in ios development..
Anchors in code is part of AutoLayout, just FYI.
Does Swift have anything similar to "Promises" or "async/await" in Javascript? And would it be possible to get a tutorial on the "dispatch" function in the future?
+MrBlaq there is a third party pod that lets you use promises
please introduce some of the best free books for swift learning??
It seems Apple releases swift book on every update.
Search “the swift programming language” in bookstore in Apple Books app.
Hi I would like to know if you do you're video from taiwan or somewhere else I can see in most video in background taiwan picture
Vacation videos from taiwan
@@LetsBuildThatApp amazing if you pass by again let me know I invited you for a drink. I'm in Taipei city, An icon of swift like you that's insane. ;)
This was really helpful. Thank you!
Thank you!
It was extremely helpful))
nicely explained, can't wait for you to talk about Generics in the future : )
very cool lesson, thank you!!!
You're a inspiration.
Thanks a lot Brian!
I think you should use white background and black text in editor for better visual.
If you're 40+ then you use white background, everyone else uses black now.
Love your videos! I'm currently learning Swift but lately have been hearing a lot about Progressive Web Applications and honestly a it does scare beginners like me learning IOS programming when people keep saying this might replace it. Can you do a video on it? I'm sure you've been hearing it a lot lately
PWA was supposed to replace native 10 years ago.....
I've been learning swift for about 3 months now and lately it's just been popping up on my youtube feed. It just gets a bit scary while you're learning native apps and these things keep popping up saying it'll replace native apps...
The most recent one is posted yesterday by CodingTech
You'll get used to this in software with every new framework that comes out
Yep and wont be the last.
This was superuseful!!!
Indeed, glad you enjoyed these more advanced concepts of Swift.
Trying to identify key components of closures: (a) the struct/func/etc. that defines the closure signature will also be the caller the closure; (b) the closure definer (or where the closure body is implemented) is where the exact VALUES for the parameters are located. This guy MUST match the defined signature, but he gets to define the implementation of the closure against the values in his scope.
Great video, thanks for posting!
Hi Brian, could you tell me which microphone you use?
+Kelvin Yan it's a rode nt1a mic
Y didnt you use filter?
Hey great video, but doesn't Swift already have a built in collection function called Filter?
Hey Brian! Thanks for the video. I am new to Swift and the concept of closures and I have a question.
Wouldn't it be more sensible to create an extension on Array for this purpose? Something like:
extension Array {
func filterList(_ isIncluded: (Element) -> Bool) -> [Element] {
var result = [Element]()
for x in self where isIncluded(x) {
result.append(x)
}
return result
}
}
So you could call it via dot notation on your Array:
let filteredList = [1, 2, 3, 4, 5, 6, 7, 8].filterList { $0 > 3 }
Thanks in advance!
As you might or might not know already, Swift already comes with the filter function out of the box for arrays. The idea of closures applies to a much broader and generic programming paradigm. Today's lesson was simply an introduction on how to write out the syntax.
Lets Build That App Thank you very much! As I'm pretty new to programming, with Swift being my first programming language (actually studying life sciences), I'm learning tons of new stuff every day and your videos help me out a lot.
@@LetsBuildThatApp How could you use the sort function if you are using a struct and are sorting one or more of the variables?
Hi Brian! Thanks for explanation. One more question - what about unwoned and weak references -> do we need to use them in every closure?
Thanks so much .helpful tutorial
Swift 4 and swift 5 what is difference please tell me today
Barely any
you saved me, thanks a lot
nicely explained
Your video is very lenthy for downloading
Cool, thanks.
Thanks a lot!
so cool, you are awesome !
Will there be more project tutorials like Twitter feed in IOS?
+John Galew I've stopped producing the Twitter feed videos
Excuse my vagueness,i meant tutorials Like Twitter and Audible
Awesome awesome vid!!!!
You're awesome too Marc!
Difficult to understand, but amazing.
You're rocking voong
Are you back in the States Brian?
Yeah, back in the States again and hopefully no more traveling.
sweet. great vid btw, very well explained.
Nicely explained... but still why is this closures so difficult to understand!
I think it's kinda like function pointer
basically are useless..... bring some part of C++ in Swift is a very bad procedure
nice video . Thanx!!
Thanks!!!
I have always wanted to see how the guys behind radio mic look when they speak like this.
Now I know :)
superb