In swift use of get and set is not compulsory and if use of "in" in closure is also not compulsory then how to differentiate a closure and computed property?
Like in below example greet is a closure or computed property?
var greet = {
return 4+3
}
greet()
2
Answers
greet
is a closure. A computed property isAnd "in" in closure is
also notcompulsory if a parameter is passed (by the way thereturn
keyword is not compulsory)unless you use the shorthand syntax
You use the keyword
in
when you need to pass a parameter.There are also differences between functions and computed properties: if you use the symbol
=
, you are equalling your variable to a function, so you need to callgreet()
.If instead
=
you use:
, you have a computed property, callgreet
.Here’s a list of different cases: