Swift constants and variables, types, collections, control flow, functions, optionals, optional chaining and guard.
42 cards · basic cards · AI-written, checked twice. Edit anything.
- In Swift, how do you declare a constant that cannot be changed?
Show answer
Use the let keyword, e.g. let name = "John"
- What keyword declares a variable in Swift?
Show answer
var
- What is type inference in Swift?
Show answer
The compiler automatically determines a variable's type based on the assigned value
- How do you explicitly specify a type in Swift?
Show answer
Use a colon followed by the type name, e.g. let count: Int = 5
- Should you use let or var for a value that never changes?
Show answer
Use let
- What is the Int type in Swift?
Show answer
A whole number with no decimal point, e.g. 42 or -17
- What is the Double type in Swift?
Show answer
A floating-point number, e.g. 3.14 or -0.5
- What is the String type in Swift?
Show answer
A sequence of characters, declared with double quotes, e.g. "Hello"
- What is the Bool type in Swift?
Show answer
A boolean value that is either true or false
- What operator checks the type of a value in Swift?
Show answer
is
- How do you cast a value to a different type in Swift?
Show answer
Use as or as? or as!, e.g. value as Int
- How do you declare an array in Swift?
Show answer
Use square brackets, e.g. let numbers = [1, 2, 3] or var items: [String] = []
- How do you access an element in a Swift array?
Show answer
Use the index in square brackets, e.g. numbers[0] gets the first element
- What does the append method do on a Swift array?
Show answer
Adds a new element to the end of the array
- How do you find the number of elements in an array?
Show answer
Use the count property, e.g. numbers.count