24
#100DaysOfSwift: Day 7
Today, I covered the second part of closures. I think its cool. Can I write it on my own? - No. Can I read the code to see if its valid? - Yes. I was doing great until I got to returning closures from functions.
When I read that this is the beginning syntax :
func travel() -> (String) -> Void
I understood that the first arrow specifies the functions return value and the second arrow specifies the closures return value. So, I read it as the function has to return a string and the closure will return nothing.
I came across this test question: This code is valid in Swift - true or false?
func paintingMaker(medium: String) -> (String) -> Void {
if medium == "watercolor" {
return {
print("I'm going to paint some flowers.")
}
} else {
return {
print("I'm going to paint a landscape.")
}
}
}
let maker = paintingMaker (medium: "watercolor")
maker()
Sorry about the format.
It was false and the reason given was : paintingMaker() says it will return a closure that accepts a string and returns nothing, but the closure it returns accepts no parameters.
That's when I realized that its read as the function value is a string and the closure value will accept it but will return nothing. Right?
Until tomorrow!
24