Section 1

Preview this deck

Shorthand number to string

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 (22)

Section 1

(22 cards)

Shorthand number to string

Front

" \(intVar)"

Back

Switches allow to filter user responses and designate an output for each one. One of these outputs is called a "switch case"

Front

"let vegetable = "red pepper" switch vegetable { case "celery": print("Add some raisins and make ants on a log.") case "cucumber", "watercress": print("That would make a good tea sandwich.") case let x where x.hasSuffix("pepper"): print("Is it a spicy \(x)?") default: print("Everything tastes good in soup.") } // Prints "Is it a spicy red pepper?"

Back

Set a default value for an optional if it returns nil.

Front

let nickName: String? = nil let fullName: String = "Johnny Appleseed" let informalGreeting = "Hi \(nickName ?? fullName)"

Back

constant keyword

Front

let

Back

Use a tuple to make a compound value- for example, to return multiple values from a function. The elements of a tuple can be referred to either by name or by number.

Front

"func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) { var min = scores[0] var max = scores[0] var sum = 0 for score in scores { if score > max { max = score } else if score < min { min = score } sum += score } return (min, max, sum) } let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9]) print(statistics.sum) // Prints "120" print(statistics.2) // Prints "120"

Back

To not require an argument label when calling a function

Front

Insert "_" before the parameter name

Back

While loops repeat a block of code until a condition changes. The conditional of the while loop is usually placed at the end so it will be run at least once

Front

"var n = 2 while n < 100 { n *= 2 } print(n) // Prints "128" var m = 2 repeat { m *= 2 } while m < 100 print(m) // Prints "128"

Back

The "func" keyword declares a function. After its initial declaration and description, you can call or reference this function by following its name with a list of its argument in parentheses

Front

"func greet(person: String, day: String) -> String { return "Hello \(person), today is \(day)." } greet(person: "Bob", day: "Tuesday")"

Back

Functions are a first-class type. This means that a function can return another function as its value.

Front

"func makeIncrementer() -> ((Int) -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne } var increment = makeIncrementer() increment(7)"

Back

Convert number to string

Front

String(numberName)

Back

How to embody a string that takes up multiple lines

Front

""" """

Back

Keep an index in a loop by using ..< to make a range of indexes (index loop) / (for loop)

Front

"var total = 0 for i in 0..<4 { total += i } print(total) // Prints "6"

Back

A function can also take another function as an argument

Front

"func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool { for item in list { if condition(item) { return true } } return false } func lessThanTen(number: Int) -> Bool { return number < 10 } var numbers = [20, 19, 7, 12] hasAnyMatches(list: numbers, condition: lessThanTen)"

Back

An optional is a container that may or may not hold a value. an optional is declared by a question mark behind the name. If an optional doesn't contain a value, it returns nill. To unpack an optional container, use an exclamation point behind the instance where you need to use the value in the container. Only unpack a container if you are sure it contains a value.

Front

"var optionalString: String? = "Hello" print(optionalString == nil) // Prints "false" var optionalName: String? = "John Appleseed" var greeting = "Hello!" if let name = optionalName { greeting = "Hello, \(name)" }" "If the optional value is nil, the conditional is false and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant after let, which makes the unwrapped value available inside the block of code."

Back

Functions can also be placed inside other functions. This is called nesting functions

Front

"func returnFifteen() -> Int { var y = 10 func add() { y += 5 } add() return y } returnFifteen()"

Back

explicitly declared double constant

Front

let explicitDouble: Double = 70

Back

for in statement?

Front

"let interestingNumbers = [ "Prime": [2, 3, 5, 7, 11, 13], "Fibonacci": [1, 1, 2, 3, 5, 8], "Square": [1, 4, 9, 16, 25], ] var largest = 0 for (kind, numbers) in interestingNumbers { for number in numbers { if number > largest { largest = number } } } print(largest) // Prints "25"

Back

variable keyword

Front

var

Back

To assign a parameter name to an argument name

Front

type the argument name in front of the parameter name

Back

implicitly declared integer constant

Front

let implicitInteger = 70

Back

Use if and switch to make conditionals

Front

"let individualScores = [75, 43, 103, 87, 12] var teamScore = 0 for score in individualScores { if score > 50 { teamScore += 3 } else { teamScore += 1 } } print(teamScore) // Prints "11"

Back

How to initialize a variable as nil

Front

var surveyAnswer: String? // surveyAnswer is automatically set to nil

Back