Section 1

Preview this deck

Variadic Parameters

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (16)

Section 1

(16 cards)

Variadic Parameters

Front

The caller can supply as many values of this parameter's type as desired, separated by comma; the function body will receive these values as an array. ex. func sayStrings(_ arrayOfStrings:String ...) { for s in arrayOfStrings { print(s) } } the three dot declares this type of parameter. A function can declare a maximum of one such parameter (because otherwise it might be impossible to determine where the list of values ends).

Back

Omission of the parameter types

Front

If the anonymous function takes parameters and their types are already known to the compiler, the types can be left out: UIView.animate(withDuration:0.4, animations: { self.myButton.frame.origin.y += 20 }, completion: { (finished) in // * print("finished: \(finished)") })

Back

Ignored Parameters

Front

A parameter whose local name is an underscore is omitted. The caller must supply an argument, but it has no name within the function body and cannot be referred to there. For example: func say(_ s:String, times:Int, loudly _:Bool) { say("hi", times:3, loudly:true)

Back

Modifiable Parameters

Front

If we want our function to alter the original value of an argument passed to it, we must do the following: • The type of the parameter we intend to change must be declared inout. • When we call the function, the variable holding the value we intend to tell it to change must be declared with var, not let. • Instead of passing the variable as an argument, we must pass its address. This is done by preceding its name with an ampersand (&). func removeCharacter(_ c:Character, from s: inout String) -> Int { var howMany = 0 while let ix = s.index(of:c) { s.remove(at:ix) howMany += 1 } return howMany } ex: var s = "hello" let result = removeCharacter("l", from:&s) another example with a class: class Dog { var name = "" } func changeName(of d:Dog, to newName: String) { d.name = newName } another example is the instance of a class: let t = Dog() t.name = "Tapi" print(t.name) changeName(of: t, to: "Toto") print(t.name) //"Toto"

Back

Omission of the parameter names

Front

If the anonymous function body doesn't need to refer to a parameter, you can substitute an underscore for its name in the parameter list in the in expression: UIView.animate(withDuration:0.4, animations: { self.myButton.frame.origin.y += 20 }, completion: { _ in // * print("finished!") })

Back

Recursion

Front

A function can call itself. This is called.... func countDownFrom(_ ix:Int) { print(ix) if ix > 0 { // stopper countDownFrom(ix-1) // *! } } countDownFrom(5) // 5, 4, 3, 2, 1, 0

Back

Omission of the return type

Front

If the anonymous function's return type is already known to the compiler, you can leave the arrow operator and the specification of the return type: UIView.animate(withDuration:0.4, animations: { () in // * self.myButton.frame.origin.y += 20 }, completion: { (finished:Bool) in // * print("finished: \(finished)") })

Back

Omission of the in expression

Front

If the anonymous function takes no parameters, and if the return type can be left, this expression itself can be omitted entirely: UIView.animate(withDuration:0.4, animations: { // * (no in line) self.myButton.frame.origin.y += 20 }, completion: { (finished:Bool) in print("finished: \(finished)") })

Back

Omission of the in expression even when there are parameters

Front

If the return type can be omitted, and if the parameter types are already known to the compiler, you can omit the in expression and refer to the parameters directly within the body of the anonymous function by using the magic names $0, $1, and so on, in order: UIView.animate(withDuration:0.4, animations: { self.myButton.frame.origin.y += 20 }, completion: { print("finished: \($0)") // * })

Back

.map(_:)

Front

method of an array takes a function that takes one parameter of the same type as the array's elements, and returns a new value Ex let arr = [2, 4, 6, 8] func doubleMe(i:Int) -> Int { return i*2 } let arr2 = arr.map(doubleMe) // [4, 8, 12, 16] Shorter: (with anonymous function) let arr = [2, 4, 6, 8] let arr2 = arr.map ({ (i:Int) -> Int in return i*2 }) Even shorter: let arr = [2, 4, 6, 8] let arr2 = arr.map {$0*2}

Back

Anonymous function

Front

To form this function, you do two things: 1. Create the function body itself, including the surrounding curly braces, but with no function declaration. 2. If necessary, express the function's parameter list and return type as the first thing inside the curly braces, followed by the keyword in. An example: UIView.animate(withDuration:0.4, animations: { () -> () in self.myButton.frame.origin.y += 20 }, completion: { (finished:Bool) -> () in print("finished: \(finished)") } )

Back

Omission of the keyword return

Front

If the anonymous function body consists of exactly one statement and that statement consists of returning a value with the keyword return, the keyword return can be skipped. To put it another way, in a context that expects a function that returns a value, if an anonymous function body consists of exactly one expres‐ sion with no return, Swift assumes that this expression's value is to be returned from the anonymous function: func greeting() -> String { return "Howdy" } func performAndPrint(_ f:()->String) { let s = f() print(s) } performAndPrint { greeting() // meaning: return greeting() }

Back

Omission of the parentheses

Front

If the parameter types are left out, the parentheses around the parameter list can be left out: UIView.animate(withDuration:0.4, animations: { self.myButton.frame.origin.y += 20 }, completion: { finished in // * print("finished: \(finished)") })

Back

In-Out Parameters

Front

If you want a function to modify a parameter's value, and you want those changes to persist after the function call has ended, define that parameter as ... instead. its getter is called as part of the function call and its setter is called as part of the function return.

Back

Closure

Front

A persistent scope which holds on to local variables even after the code execution has moved out of that block. Languages which support closure (such as JavaScript, Swift and Ruby) will allow you to keep a reference to a scope (including its parent scopes), even after the block in which those variables were declared has finished executing, provided you keep a reference to that block or function somewhere.

Back

Omission of the function argument label

Front

If, as will just about always be the case, your anonymous function is the last argument being passed in this function call, you can close the function call with a right parenthesis before this last argument, and then put just the anonymous function body without a label (this is called a trailing function): UIView.animate(withDuration:0.4, animations: { self.myButton.frame.origin.y += 20 }) { // * _ in print("finished!") }

Back