TTS Swift Notes: The Basics Flashcards
What does Int stands for?
Integer
A Double accounts for what?
A floating-point value up to 15 spaces after the decimal.
A Float accounts for what?
Numbers with decimals at least 6 spaces after the decimal.
What does Bool stand for?
Boolean
What does a String represent?
Textual data.
What do tuples do?
They enable the creation and passing around of groupings of values. You can use a tuple to return multiple values from a function as a single compound value.
Optionals return what two values?
There is a value and it equals x OR there isn’t a value at all.
What types do optionals work for?
All, not just classes.
Can the value of a constant be changed once it’s been set?
No.
What keyword declares a constant?
let
What keyword declares a variable?
var
What the is proper way to name a variable/constant? currentLoginAttempt or current_login_attempt?
The first.
Can you declare multiple constants or variables on a single line?
Yes
How do you declare multiple constants or variables on a single line?
By separating them with commas.
What does type annotation do?
It is clear about the kind of values a constant or variable can store.
How do you write a type annotation?
By placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use. EX: var welcomeMessage: String
How do you read the following?: var welcomeMessage: String
“Declare a variable called welcomeMessage that is of type String.”
Can you define multiple related variables of the same type on a single line?
Yes
How do you define multiple related variables of the same type on a single line?
Separate the variables by commas, follow the last variable with a colon, then declare the type annotation. EX: var red, green, blue: Double
If you do not declare a type, how does Swift know what type it is?
It infers it.
What five items can constant and variable names not contain?
- Whitespace characters
- Mathematical symbols
- Arrows
- Private-use (or invalid) Unicode code points
- line- and box-drawing characters
What can constant and variable names not start with?
A number.
Once you’ve declared a constant or variable of a certain type, can you redeclare it again with the same name or change it to store values of a different type?
No.
Can you change a constant into a variable or vice versa?
No.
What command prints the current value of a constant or variable?
print()
Swift uses string interpolation to do what?
To include the name of a constant or variable as a placeholder in a longer string and to prompt Swift to replace it with the current value of that constant or variable.
How do you call string interpolation?
()
How do you comment out lines?
//
How do you comment out multiple lines?
/* */
How do you nest comments?
Start a multiline comment block and then start a second multiline comment within the first block.
/* this is the start of the first multiline comment /* this is the second, nested multiline comment */ this is the end of the first multiline comment */
What are required if you want to write multiple separate statements on a single line?
semicolons
- let cat = “🐱”; print(cat)
- // prints “🐱”
What are integers?
Whole numbers with no fractional component, such as 42 or -23.
What is a signed integer?
One that is positive, zero, or negative.
What is an unsigned integer?
One that is positive or zero.
Swift provides signed and unsigned integers in what four bit forms?
8, 16, 32, and 64.
What does UInt8 stand for?
8-bit unsigned integer.
What does Int32 stand for?
A 32-bit signed integer.
What is the minValue of UInt8.min?
0
What is the max value of UInt8.max?
255
What-bit floating-point number does a Double represent?
64-bit
What-bit floating-point number does a Float represent?
32-bit
How does Swift assign floating-point numbers by default?
As a Double.
If you combine an integer and a floating-point literals in an expression, what type will Swift assign it?
As a Double.
In what four ways can an integer literal be written?
- A decimal number, with no prefix
- A binary number, with a 0b prefix
- An octal number, with a 0o prefix
- A hexadecimal number, with a 0x prefix
How do you write an exponent?
With an uppercase or lowercase e. EX: 1.25e2 means 1.25 X 10squared
Both integers and floats can be padded with what two things to improve readability?
Extra zeros and underscores. EX: 000123.456, 1_000_000
What are the four reasons to use specific integer types (i.e., not just Int)
If there is explicitly-sized data from an external source, for performance, for memory usage, or for other necessary optimization,
An Int8 constant or variable can store numbers between what and what?
-128 and 127
An UInt8 constant or variable can store numbers between what and what?
0 and 255
How do you convert one specific number type to another?
Initialize a new number of the desired type with the existing value.
What do type aliases do?
They define an alternative name for an existing type.
How do you define type aliases?
With the type alias keyword.
When are type aliases useful?
When you want to refer to an existing type by a name that is contextually more appropriate, such as when working with data of a specific size from an external source.
What two Boolean constant values does Swift provide?
true and false
What kind of values can a tuple have?
Any type and they do not have to be of the same type as each other.
Can you name the individual elements in a tuple?
Yes
When can you name the individual elements in a tuple?
When the tuple is defined.
What is useful about naming tuple elements?
You can use the element names to access the values of those elements.
What is particularly useful about tuples?
They are useful as the return values of functions.
Are tuples for temporary or permanent groups of related values?
Temporary.
If your data structure is likely to persist beyond a temporary scope, what are the best ways to model it?
As a class or as a structure.
How is an option Int written?
Int?
How do you set an optional variable to a valueless state?
Assign it the special value nil.
If you define an optional variable without providing a default value, what is the variable automatically set to?
nil
In Swift, what is nil?
Nil is the absence of a value of a certain type.
What is forced unwrapping?
Accessing an underlying value by adding an exclamation point (!) to the end of an optional’s name. Effectively saying, “I know that this optional definitely has a value; please use it.”
When should you use optional binding?
To find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable.
When is it useful to remove the need to check and unwrap the optional’s value every time it is accessed?
When it is clear from a program’s structure that an optional will always have a value, after that value is first set.
What is an implicit unwrapped optional?
An optional that will always have a value.
How do you write an implicitly unwrapped optional?
By placing an exclamation mark (String!) after the type that you want to make optional.
When do you place an exclamation point after an optional’s type?
When you declare it.
When is it not a good idea to use an implicit unwrapped optional?
If there is a possibility of a variable becoming nil at a later point.
What is an assertion?
A runtime check that a Boolean condition definitely evaluates to true.
When do you use an assertion?
To make sure that an essential condition is satisfied before executing any further code.
What does an assertion let you provide?
A suitable debug message as to the nature of the assert.
How do you write an assertion?
By calling the Swift standard library global assert(_:_file:line:)
- let age = -3
- assert(age >= 0, “A person’s age cannot be less than zero”)
// this causes the assertion to trigger, because age is not >= 0