Swift Flashcards
Escape variable from a string
“Model is (model)”
variable vs const
var and let
what are the data types
Int, Double, Float, String, Bool
Define a tuple, and access the second value
let myTuple = (1, 15, “Test”)
var val = myTuple.1
Deconstruct a tuple.
let (x, _, z) = myTuple
Define a tuple with variable names, and access the second value
let myTuple = (first: 1, second: 2, str: “test”
var val = myTuple.second
What happens when using a variable that has a non-optional type, but hasn’tbeen assigned a value?
An error stating that the value is being used before being initialized.
It is not nil.
Define an optional Int and use it
let x: Int?
if (x != nil) {
print(x!)
}
Unwrap multiple optionals
if let x = opt1, let y = opt2, let z = opt3 {
}
Difference between Int? and Int! when declaring variables
? requires forced unwrapping. AKA using myVal! when accessing or using let x = myVal in if condition.
! means it is implicitly unwrapped and can be used without checking if nil
Can var myString: String = nil be used?
No, only optionals can be nil
Upcast UIButton to UIControl
let myControl = myButton as UIControl
Downcast UIScrollView to UITextView
if let myTextView = myScrollView as ? UITextView {
}
CHeck if something is a Class
if myObj is Class {
}