Term
Define a restraunt "Lombardi's" that serves pizza with pesto, bacon, and cheddar cheese
struct Pizza { let ingredients: [String] }
protocol Pizzeria { func makePizza(_ ingredients: [String]) -> Pizza func makeMargherita() -> Pizza } extension Pizzeria { func makeMargherita() -> Pizza { return makePizza(["tomato", "mozzarella"]) } } |
|
Definition
struct Lombardis: Pizzeria { func makePizza(_ ingredients: [String]) -> Pizza { return Pizza(ingredients: ingredients) } func makeMargherita() -> Pizza { return makePizza(["tomato", "basil", "mozzarella"]) } }
To call: let lombardis: Pizzeria = Lombardis() |
|
|
Term
Why does this code throw a compile-time error?
struct Kitten { }
func showKitten(kitten: Kitten?) { guard let k = kitten else { print("There is no kitten") } print(k) } |
|
Definition
The else block of a guard requires an exit path, either by using return, throwing an exception or calling a @noreturn.
1. func showKitten(kitten: Kitten?) { guard let k = kitten else { print("There is no kitten") return } print(k) }
2. Instead of 'return', throw fatalError()
3. enum KittenError: Error { case NoKitten }
struct Kitten { }
func showKitten(kitten: Kitten?) throws { guard let k = kitten else { print("There is no kitten") throw KittenError.NoKitten } print(k) }
try showKitten(kitten: nil) |
|
|
Term
What is unique about Sets? |
|
Definition
aaaThey are an array of objects that are automatically unordered, and duplicates are ignored |
|
|
Term
|
Definition
Tuples are a type of variable that can store multiple values stored in a single value
var name = (first: "Taylor", last: "Swift") |
|
|
Term
|
Definition
Enumerations are a way to define groups of related values to make them easier to use.
enum Result{
case success
case failure
}
let result = Result.failure |
|
|
Term
What are Enumeration associated values? |
|
Definition
enum Activity{
case bored
case running(destination: String)
case talking(topic: String)
}
let talking = Activity.running(destination: "Africa") |
|
|
Term
Write a function that takes a parameter Int, returns a String
Call it. |
|
Definition
func format(number: Int) -> String{
return "bla"
}
To call:
let bla = format(number: 3) |
|
|
Term
Write a function that takes a parameter Int and returns a String. Use _
Call it. |
|
Definition
func format(_ number: Int) -> String{
return String(number)
}
To call:
let bla = format(3) |
|
|
Term
What are Inouts?
Write a function that uses InOuts.
Call the function. |
|
Definition
Inouts are functions where you can change the parameter inside them.
func double(number: inout Int){
number *= 2
}
To call:
var bla = 10
double(&10) |
|
|
Term
What are Closures?
Write a closure.
Call it |
|
Definition
A closure is a function assigned to a variable
let driving = {
print("bla")
}
To call:
driving() |
|
|
Term
What is a struct?
Is it a reference type or a value type? What do each mean?
What is the difference between a struct and a class?
What do you call a function defined inside a struct?
Write a struct Sport with a name property
Call it. |
|
Definition
A struct is a data type that can store values, and can define functions. They can define initializers to set up their initial state with init()
A struct is a value type. When you copy a value type, each instance keeps a unique copy of the data. When you copy a reference type, each instance SHARES the data. When you change one, the other changes too.
A class can create a subclass that will inherit the paren't properties and methods, whereas structs don't support inheritance. A class has all members private by default. A struct is a class where members are public by default.
A function inside a struct is called a method.
struct Sport(){
var name: String
}
var tennis = Sport(name: "tennis") |
|
|
Term
Remember that structs are value types, which means they are supposed to be immutable.
Say we have a struct
struct Rectangle(){
var width = 2
var height = 2
}
Write a function inside the struct that doubles the width and height of the rectangle. |
|
Definition
struct Rectangle(){
var width = 2
var height = 2
mutating func makeDouble(){
width *= 2
height *= 2
}
} |
|
|
Term
What is a class?
Is it a reference type or a value type? What do each mean?
What is the difference between a class and a struct?
Write a class Sport with a name property
Call it. |
|
Definition
A class is a data type that can store values, and can define functions. They can define initializers to set up their initial state with init()
A class is a reference type. When you copy a reference type, each instance SHARES the data. When you change one, the other changes too. A struct is a value type. When you copy a value type, each instance keeps a unique copy of the data.
A class can create a subclass that will inherit the paren't properties and methods, whereas structs don't support inheritance. A class has all members private by default. A struct is a class where members are public by default.
class Sport(){
var name: String
}
var tennis = Sport(name: "tennis") |
|
|
Term
Write a class Dog with properties name, age, with initial values of "Doberman" for name, "14" for age.
Write function printWoof that prints "Woof". |
|
Definition
}
class Dog{
var name: String
var age: Int
init(){
name = "Doberman"
age = 14
}
func printWoof(){
print("woof")
}
} |
|
|
Term
Simplify a function that adds two values together using $0 and $1
var add = {(arg1: Int, arg2: Int) -> Int in
return arg1 + arg2
} |
|
Definition
var add = {(arg1: Int, arg2: Int) -> Int in
return arg1 + arg2
}
add = {(arg1, arg2) -> Int in
return arg1 + arg2
}
add = {
$0 + $1
}
let result = add(20, 20)
|
|
|
Term
Simplify a function that adds two values together using $0 and $1
var add = {(arg1: Int, arg2: Int) -> Int in
return arg1 + arg2
} |
|
Definition
var add = {(arg1: Int, arg2: Int) -> Int in
return arg1 + arg2
}
add = {(arg1, arg2) -> Int in
return arg1 + arg2
}
add = {
$0 + $1
}
let result = add(20, 20)
|
|
|
Term
What are generics and which problem do they solve? |
|
Definition
It solves the problem of code duplication.
|
|
|
Term
What does the 'lazy' keyword do? |
|
Definition
Makes it so that the var doesn't initialize until someone tries to grab it |
|
|