The Basics Flashcards
How do you declare variables and constants?
How do you declare multiple constants/variables at the same time
How do you provide a type annotation?
var welcomeMessage: String
How do you use type annotation to define multiple related variables?
var red, green, blue: Double
How does print() work?
let friendlyWelcome = “Bonjour”
print(friendlyWelcome) // printss “Bonjour”
To use that value in a string:
print(“The current value of friendlyWelcome is (friendlyWelcome)”)
How do you perform an integer conversion with the following in order to add them together:
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
Create a new constant of the type we want the number to use.
let twoThousandAndOne = twoThousand + UInt16(one)
How would you convert Integer to Floating-Point for the following so we can add them together?
let three = 3;
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
How do you convert a floating-point to integer and how does it work with the decimal points? Use Pi as an example.
How do you safely work with an optional in Swift?
How does Optional Binding work?
It assigns an optional values to a variable after checknig to make sure there is a value there.
How do you include multiple optional bindings and boolean conditions in a single if statement?
They need to be seperated by commas, if any of the values in the optional bindings are nil or any Boolean condition evaluaties to false, the whole if statement’s coniditon is considered to be false. Example below:
In general, why would you want to implicitly unwrap an optional?
Sometimes it’s clear from a programs’ structure tat an optiona will always have a value, after that value is first set. In these cases it’s useful to remove the need to check and unwrap the optional’s value every time it’s accessed, because it can be safely assumed to have a value all the time.
You can think of an implicitly unwrapped optional as giving permission for the optional to be unwrapped automatically whenever it’s used.
What is the difference between Assertions and Preconditions?
How would you write an assertion regarding ensuing age is >= 0?
What is an alternate way to use assertions with conditionals?
using the assertionFailture( ) function. It forces an assertion fail with the text you enter:
How do you write a precondition?
put in a condition that must be true
precondition(index > 0, “Index must be greater than zero,”)