You can refer to parameters by number instead of by name—this approach is especially useful in very short
closures.
Front
TRUE
Back
declare an optional
Front
var optionalString: String? = "Hello"
Back
the constructor method for a class is named 'init'
Front
class c {
var name: String
init(name: String) {
self.name = name
}
Back
"create for-in loop to iterate over items in a dictionary by providing a pair of names to use for each key-value
pair."
Front
let d = [
"x":[1,2,3],
"y":[4,5,6],
"z":[7,8,9]
]
for (k,n) in x {
for m in n {...}
}
Back
create a function that returns a tuple
Front
func f() -> (Int, Int, Int) { return (1,2,3)}
Back
create a function that takes another function as one of its arguments
Front
func matches(condition: Int -> Bool) -> Bool {
if condition(item) { return true }
return false
}
Back
add a new key and value to a dictionary
Front
d1["newkey"] = "newvalue"
Back
name four reserved words that cause loops
Front
for-in, for, while, and do-while
Back
example of switch statement
Front
let vegetable = "red pepper"
switch vegetable {
case "celery":
let a = 1
case "cucumber", "watercress":
let b = 2
case let x where x.hasSuffix("pepper"):
let c = 3
default:
let d = 4
}
Back
A closure passed as the last argument to a function can appear immediately after the parentheses.
Front
sort([1, 5, 3, 12, 2]) { $0 > $1 }
Back
use if let on an optional
Front
var optionalName: String? = "John"
if let name = optionalName {
greeting = "Hello, \(name)"
}
Back
You have several options for writing closures more concisely. When a closure's type is already known, such as
the callback for a delegate, you can omit the type of its parameters, its return type, or both.
Front
TRUE
Back
statements do not need a semicolon at the end
Front
TRUE
Back
code at the global scope is the main() function
Front
TRUE
Back
name two reserved words that are conditionals
Front
if and switch
Back
declare a variable
Front
var v
Back
Functions are actually a special case of closures. You can write a closure without a name by surrounding code with braces { }. Use 'in' to separate the arguments and return type from the body.
Front
numbers.map({
(number: Int) -> Int in
let result = 3 * number
return result
})
Back
Values are never implicitly converted to another type
Front
TRUE. example: let s = label + String(int1)
Back
declare a constant
Front
let c
Back
example of for-in loop
Front
let scores = [2,6,8]
for score in scores {
....
}
Back
create a constant that is explicitly typed
Front
let c : Double = 70
Back
check if optional is nil
Front
optionalString == nil
Back
create a new variable that will be assigned a dictionary value later
Front
var d = [ : ]
Back
use if and let together to work with a variable whose value might be missing
Front
if let x = obj1 { x.name = "abc" }
Back
create an instance of a class by putting parentheses after the class name
Front
var shape = Shape()
Back
"You use for-in to iterate over items in a dictionary by providing a pair of names to use for each key-value
pair."
Front
let nums= [
"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 nums{
for number in numbers {
if number > largest {
largest = number
}
}
}
Back
Use deinit to create a deinitializer if you need to perform some cleanup before the object is deallocated
Front
TRUE
Back
create a function that takes a variable number of arguments
Front
func f(numbers: Int...) { }
Back
write a string that has an integer variable in it
Front
"I have \(apples) apples."
Back
Create a single statement
closure that implicitly returns the value of its only statement.
Front
numbers.map({ number in 3 * number })
Back
declare and initialize a dictionary
Front
var x = ["key1": "val1","key2": "val2"]
Back
create while loop to iterate 10 times
Front
var n = 0
while n < 10 {
n = n + 1
}
Back
create a new dictionary
Front
let d = Dictionary<String, Float>()
Back
declare and initialize a string array
Front
var x= ["abc", "def", "ghi"]
Back
Switches support any kind of data and a wide variety of comparison operations—they aren't limited to integers and tests for equality.