Stanford Swift Flashcards
Give a good example of unwrapping
// NB ** GOOD EXPLANATION OF UNWRAPPING *** unwrap this as the result is an optional in the brain (ie the MODEL). The code below basically means if the result is determined, then you will let the displayValue equal the result (ie the brain.result, but we can’t use this direclty as this could be an optional!) .
if let result = brain.result { displayValue = result }
What is an unary operations?
What is a binary operation?
Unary Operation: eg square root. Performs an operation on one operand (ie the number being operation on)
Binary Operation: eg multiplication. Performs operation on multiple operations (ie multiplying two numbers together)
What is optional defaulting?
eg 1
// Optional defaulting works as follows. If the display is blank you want a space
let s: String? = ... // might be nil if s != nil { display.text = s } else { display.text = " " }
// can be expressed as
display.text = s ?? “ !
What are tuples?
eg 1 (least used)
// A tuple is a grouping of values. You can use it anywhere you’re using a type.
let x: (String, Int, Double) = ("hello", 5, 0.85) // the type of x is "a tuple".
let (word, number, value) = x // this names the tuple elements when accessing the tuple
print(word) // prints hello
print(number) // prints 5
print(value) // prints 0.85
What are tuples?
eg 2 (most used)
// the tuples elements can be named when the tuple is declared (this is strongly preferred)…
let x (w: String, i: Int, v: Double) = ("hello", 5, 0.85) print(x.w) // prints hello print(x.i) // prints 5 print(x.v) // prints 0.85
Explain tuples as return values
You can use tuples to return multiple values from a function or method.
func getSize() -> (weight: Double, height: Double) { return (250, 80) }
let x = getSize() print("weight is \(x.weight)") // weight is 250
// …or
print(“height is (getSize().height)”) // height is 80
What is a Range in Swift?
//A range in Swift is just two end points
// A range can represent things like a selection in some text or a portion of an Array.
// Range is generic (eg Range ), but T is restricted (eg comparable - the start index has to be less than the end index). This is sort of a pseudo-representation of Range…it’s a bit more complicated. You can have a range of ints, floats, and even strings.
struct Range {
var startIndex: T
var endIndex: T
}
// Syntax for created a Range ..< // exclusive of the upper bound or ... (inclusive of both bounds)
What is a CountableRange
A CountableRange contains consecutive values which can be iterated over or indexed into
Give an example of creating a Range
let array = ["a", "b", "c", "d"] let a = array[2...3] // a will be a slice of the array c and d let b = array[2..<3] // will be a slice of the array c
CountableRange is enumeratable with ‘for in’
Give an example of this
// Count through/enumerate through the sequence
for (i = 0; i < 20; i++) // loop in C-like language
for i in 0..<20 {
}
you can ‘for in’ through different sequences - ie countableRange, arrays, dictionaries
Do floating point numbers stride by Int?
No, they stride by floating points value.
So 0.5…15.25 is just a range, not a CountableRange (which is needed for ‘for in’)
There’s a global function that will create a countable range from floating point values
for i in stride(from:0.5, through: 15.25, by: 0.3) {
}
Return type of stride is ClosedCountableRange
What are the four data structures in Swift?
Classes, Structures, Enumerations, and Protocols. Only difference is a Class can specify a Superclass.
eg in Calculator // declaration syntax class ViewController { } struct calculator brain { } enum operation { } // Similarities They can all have properties and functions, but there can't be any stored properties in an enum. Enums keep any data it has in associated values. It can have computed properties however. Structs and Classes have initialisers apart from enums, as you just say the case you want. // Differences Classes are the only ones that have inheritance. nb Structs and Enums are Value types, and Classes are Reference types
What is a value type? (eg struct and enum)
A value type is…
- Copied when passed as an argument to a function
- Copied when assigned to a difference variable
- Immutable if assigned to a variable with let (function parameters are let). You must note any func that can mutate a struct/enum with the keyword mutating
What is a reference type? (eg class)
A reference class gets stored in the heap with a pointer to it
- Stored in the heap and reference counted (automatically)
- Constant pointers to a class (let) still can mutate by calling methods and changed properties (the pointer can’t be changed but what it points to can).
- When passed as an argument, does not make a copy (just passing a pointer to some instance).
When would you use a class, vs struc or enum
ie reference types vs value types
Use of enum is situational - any time you have a type of data with discrete values.