Skip to main content

Swift - Closures

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.
  1. func someFunctionThatTakesAClosure(closure: () -> Void) {
  2. // function body goes here
  3. }
  4. // Here's how you call this function without using a trailing closure:
  5. someFunctionThatTakesAClosure(closure: {
  6. // closure's body goes here
  7. })
  8. // Here's how you call this function with a trailing closure instead:
  9. someFunctionThatTakesAClosure() {
  10. // trailing closure's body goes here
  11. }

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

Popular posts from this blog

Swift - Structure and Classes

Structures and Classes: How are structures different ? No inheritance.  No type casting to check at runtime.  No reference counting. No multiple references.  No deinitializers.  Structures are value types. When you assign instance of a struct to another variable, the entire structure is COPIED and not REFERENCED. Classes instead are REFERENCE types, pointing to the same instance.  === operator is to know if two variables / constants refer to the same instance.  == means they are equal in some value. It is your responsibility to implement == and != in your class.

Swift - Functions

Functions: A chunk of code that performs an action or specific task. Can have in-out parameters. Can have no-parameters to complex parameters of different types. When a function say it will return a value, it must return a value by the end of function execution. But the function call can ignore return value. Functions can return multiple values using Tuple. Function parameters are constant by default., if you try to change the value of a parameter it will give a compiler error. If you ever want to modify a parameter value, mark them as in&out parameter type. InOut parameters must be variables., because constants can not be modified.  Variadic parameters can not marked as InOut. Place & before variable, while calling the function.  Write inout before parameter type while defining the function. InOut parameters is a way to have an impact of the function out side of its scope. func swapTwoInts ( _ a : inout Int , _ b : inout Int ) var someInt = 3 var an

Swift 5

Designed for safety: Swift is designed for safety. By default, Swift objects can never be nil. But there are scenarios where nil is valid and needed. To handle such, innovative approach called Optionals. Swift syntax forces you to use ? to safely deal. Cases: 1. A collection can be empty.