-
String interpolation
A way in Swift to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. Each item that you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash.
-
Set
A collection that stores distinct elements with no defined order
var favoriteGenres: _?_<String> = [“Rock”, “Classical”, “Hip hop”]
-
Closed Range Operator
An operator (...) that lets you create a range of numbers that includes both the lower and upper values. used in for loops:
for num in 1…5 {
print(“(num) Nmes 4 is (num * 4)”)
}
-
Closure
Self-contained blocks of functionality that can be passed around.
let displayGreeting = {print(“Hello Class”)}
let displayGreetng: () -> () = {print(“Hello Class”)}
displayGreeting()
-
Optionals
Handle the absence of a value– There is a value and it equals x or there isn’t a value
var numberOfLegs = [“ant”: 6, “snake”: 0, “cow” :4]
let possibleNumLegs = numberOfLegs[“goat”] ???
let possibleNumLegs: Int? = numberOfLegs[“goat”] //Value or nil
if possibleNumLegs != nil {
let legCount = possibleNumLegs! //Use ! to unwrap the optional
print(“Goat has (legCount) legs”)
}
-
Short cut for this optional would look like?
var numberOfLegs = [“ant”: 6, “snake”: 0, “cow” :4]
let possibleNumLegs = numberOfLegs[“goat”] ???
let possibleNumLegs: Int? = numberOfLegs[“goat”] //Value or nil
if possibleNumLegs != nil {
let legCount = possibleNumLegs! //Use ! to unwrap the optional
print(“Goat has (legCount) legs”)
}
let possibleNumLegs = numberOfLegs[“goat”]
if let legCount = possibleNumLegs {
print(“Goat has (legCount) legs”)
}
-
half-open range operator (a..<b)
Defines a range that runs from a to b, but does not include b.
It is said to be half-open because it contains its first value, but not its final value. As with the closed range operator, the value of a must not be greater than b. If the value of a is equal to b, then the resulting range will be empty.
-
(T/F)Swift Strings cannot be indexed by integer values.
TRUE
Explanation: Different characters require different amounts of storage. Instead, you traverse over each Unicode scalar from the start or end of the string.
-
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
What does this print?
G
-
let greeting = "Guten Tag!"
greeting[greeting.endIndex.predecessor()]
What does this code access?
!
-
let greeting = "Guten Tag!"
greeting[greeting.startIndex.successor()]
What character does this code access?
u
-
let index = greetng.startIndex.advancedBy(7)
greeting[index]
What character of the code does this access?
a (you might think T but its startIndex + 7)
-
Self-contained pieces of code to perform a task.
Function
-
In functions what are the differences between external and local parameter names
- – External Parameter labels arguments passed to a function call
- – Local Parameter Name used in implementation of function.
func someFunction(1, secondParameterName: 2)
-
(T/F) In functions, You may omit the external name for all arguments.
TRUE
Implementation:
func someFunction(firstParameterName: Int, _ secondParameterName: Int)
call:
someFunction(1, 2)
-
Write a function interface with a default parameter.
func someFunction(parameterWithDefault: Int = 12)
-
Variadic Parameters
Allows for zero or more values for input parameter. Made available as an array.
func arithmetiMean(numbers: Double...) -> Double
-
In-Out Parameters
Gives the ability to change the value of a parameter:
definition:
func swapTwoInts(inout a: Int, inout _ b: Int) {...}
function call:
swapTwoInts(&someInt, &anotherInt)
-
(T/F)In swift the following line of code:
var mathFunction: (Int, Int) -> Int = addTwoInts
is legal?
TRUE
func addTwoInts(a: Int, _ b: Int) -> Int {return a + b}
-
Write an implementation for a nested function:
func chooseStepFunction(backwards: Bool) -> (Int) -> Int {
func stepForward(input: Int) -> Int { return input + 1 }
func stepBackward(input: Int) -> Int { return input - 1 }
return backwards ? stepBackward : stepForward
}
|
|