Swift Fundamentals Flashcards
What’s the difference between mutable and immutable ?
A mutable object allows for change. An immutable object does not allow for changes.
What is a property observer?
A property observer listens for changes on an object. One can listen for changes when the object is about to get set and when the object actually got set.
What is a computed property ?
A computed property returns the value of a block of calculated logic.
What are higher order functions?
A function that takes another function as an argument or returns a function is said to be a higher order function. This is the fundamental pillar of functional programming.
What is recursion?
A function that calls itself. The two main parts of a recursive function is the base case and the recursive call.
What are access control / modifiers and give three examples?
Access control provide varied level of access to parts of the code of an object from another source object.
private
public
internal
Name three built-in protocols in Swift and their use cases?
Hashable. Types conforming to Hashable will be guaranteed to be unique.
CaseIterable. Enums conforming to CaseIterable will make all their cases available and iterable.
CustomStringConvertible. Conforming to CustomStringConvertible allows a type to override the description property on an object and return a custom String.
What’s the benefit of an inout function?
To be able to mutate via referencing the data outside the scope of a function.
What is an optional ?
In Swift an optional is a type used to indicate that an object can or not have a value.
What are Closures ?
Closures are anonymous functions (functions without a name) that capture references to values in their surrounding context. This is one of the subtle differences between functions and closures. Please note however that nested functions also capture their surrounding values.
What is GCD?
Grand central dispatch is the library that iOS uses to handle concurrency.
Name the types of loops available in Swift ?
while, for-in, and repeat-while
If using a Command-line macOS application what’s the function used for taking user input ?
For user input or STDIN when working in a command-line application we use readLine().
What is the restriction on a dictionary ?
The keys need to conform to Hashable.
What is Object Oriented Programming ?
A paradigm used in programming to represent objects and encapsulate their properties and functions.
// Parent class class Person { var name: String var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func info() { print("Hi, my name is \(name)") } }
// Fellow inherits from the Person class // Subclass class Fellow: Person {}
let fellow = Fellow(name: "Xavier Li", age: 23) fellow.info() // Hi, my name is Xavier Li