How Do You Write Closures in Swift?

preview_player
Показать описание
Closures are used everywhere in Swift and the UIKit APIs that we have access to. Today we'll go through a simple exercise of how to write and apply closures by going through a simple algorithm exercise. This lesson covers how to write filtering functions using closures to make our programs more robust. Have fun.

Instagram Firebase Course

Facebook Group

iOS Basic Training Course

Completed Source Code

Рекомендации по теме
Комментарии
Автор

FINALLY someone who made me understand closures, thank you!

PedrocaRicardo
Автор

I just tested this code:
func (Int) -> Bool, numbers: [Int]) -> [Int]{
let greater = numbers.filter(closure)
return greater
}

{
return $0 > 6
}, numbers: [1, 2, 3, 4, 5, 10])

It successfully run. :D Thanks Brian.

kaiseration
Автор

Eventually someone decided to make closure understandable and easy to use thank you sir !

mutasemwahbeh
Автор

Now I seem to be getting pretty much closer to the point of understanding WHY do we need closures)) Thanks a lot!

antonlebedev
Автор

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👌🏾

generalk
Автор

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 But on the flip side, everyone who looked at the function knew it's exact purpose.

DrZiggyzoo
Автор

Bro... you save me from understanding closure! Thanks a lot!

Mulyono-bb
Автор

Best tutorial for Closures so far. Thank you man.

emindeniz
Автор

Hi Brian, great video. However I think you should have mentioned trailing closures too, as it simplifies the code quite a bit.

func [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.

avisekhon
Автор

One of my favorite youtubers! Awesome stuff!

maxgebhardt
Автор

I love these kind of videos, where you explore the code syntax for advanced stuff. Nice and clean, thank you brian!

developerios
Автор

Thank you very much for making this tutorial video and, for making it with simple explanations so I can understand it. I love it !!

Pedruchito
Автор

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

augustogalindo
Автор

Best tutorial on closures !!!! I want to learn more about @escaping closures.

rohanghorpade
Автор

nice work with the audio, real pro level stuff.

---mljd
Автор

Thanks paused the video and used a closure to solve some mases in that ios swift game. Was a lot of fun.

liminal
Автор

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

SevenDeMagnus
Автор

Beautiful closure explanation..!! Thank you!

israman
Автор

Really needed this! Many thanks! You have yourself a new subscriber.

BreatheDigital
Автор

Another great video, everyday you surprise me with what I can learn, thanks !!

Noahnrg