Closures:
1. These are self contained blocks of code that can be passed around in the code.
Declare:
(parameters) -> return
Define:
{ (parameters) -> return type in
// executable statements
}
reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 } )
1. Inferring type from context
reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )
2. Implicit returns from Single-expression closures
reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )
3. Short hand argument names. Can omit variable name, return and in key word as well.
reversedNames = names.sorted(by: { $0 > $1 } )
4.Operator methods. Simply use the operator.
reversedNames = names.sorted(by: >)
5.Trailing closures. If closure is the final argument of a function, it can be passed as a trailing closure.
6. Captures values. Closures capture values from surrounding context in which it is defined. Nested functions capture values from the outer function.
7. Closures are reference types. Even if you declare a closure as let, still it can update variables that are enclosed with in its context. A let closure means you can't assign other closure to it.
7. Escaping Closures:
A closure is set to escape, when it is passed to a function as an argument and if the function returns before closure being called, then the closure is called escaping closure.
Refer self explicitly.
8. Auto Closures. Don't worry about this for now.
1. These are self contained blocks of code that can be passed around in the code.
Declare:
(parameters) -> return
Define:
{ (parameters) -> return type in
// executable statements
}
reversedNames = names.sorted(by: { (s1: String, s2: String) -> Bool in return s1 > s2 } )
1. Inferring type from context
reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )
2. Implicit returns from Single-expression closures
reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )
3. Short hand argument names. Can omit variable name, return and in key word as well.
reversedNames = names.sorted(by: { $0 > $1 } )
4.Operator methods. Simply use the operator.
reversedNames = names.sorted(by: >)
5.Trailing closures. If closure is the final argument of a function, it can be passed as a trailing closure.
- func someFunctionThatTakesAClosure(closure: () -> Void) {
- // function body goes here
- }
- // Here's how you call this function without using a trailing closure:
- someFunctionThatTakesAClosure(closure: {
- // closure's body goes here
- })
- // Here's how you call this function with a trailing closure instead:
- someFunctionThatTakesAClosure() {
- // trailing closure's body goes here
- }
6. Captures values. Closures capture values from surrounding context in which it is defined. Nested functions capture values from the outer function.
7. Closures are reference types. Even if you declare a closure as let, still it can update variables that are enclosed with in its context. A let closure means you can't assign other closure to it.
7. Escaping Closures:
A closure is set to escape, when it is passed to a function as an argument and if the function returns before closure being called, then the closure is called escaping closure.
Refer self explicitly.
8. Auto Closures. Don't worry about this for now.
Comments
Post a Comment