Swift Fundamentals Flashcards

1
Q

What’s the difference between mutable and immutable ?

A

A mutable object allows for change. An immutable object does not allow for changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a property observer?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a computed property ?

A

A computed property returns the value of a block of calculated logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are higher order functions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is recursion?

A

A function that calls itself. The two main parts of a recursive function is the base case and the recursive call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are access control / modifiers and give three examples?

A

Access control provide varied level of access to parts of the code of an object from another source object.

private
public
internal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Name three built-in protocols in Swift and their use cases?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the benefit of an inout function?

A

To be able to mutate via referencing the data outside the scope of a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an optional ?

A

In Swift an optional is a type used to indicate that an object can or not have a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are Closures ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is GCD?

A

Grand central dispatch is the library that iOS uses to handle concurrency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Name the types of loops available in Swift ?

A

while, for-in, and repeat-while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

If using a Command-line macOS application what’s the function used for taking user input ?

A

For user input or STDIN when working in a command-line application we use readLine().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the restriction on a dictionary ?

A

The keys need to conform to Hashable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Object Oriented Programming ?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Name three principles of OOP ?

A

Inheritance, Encapsulation and Polymorphism.

Encapsulation means that objects keep their state information private. Rather than directly manipulating an object’s data, other objects send requests to the object, in the form of messages, some of which the object may respond to by altering its internal state.

Polymorphism means that objects of different classes can be used interchangeably. This is especially important, as it allows you to hook up classes at a later date in ways you didn’t necessarily foresee when those classes were first designed.

Inheritance means that objects of one class can derive part of their behavior from another (base or parent) class. Some object-oriented languages (C++, for example, but not Swift) allow multiple inheritance, where objects of one class can derive part or all of their behavior from multiple independent base classes.

17
Q

What is Protocol Oriented Programming ?

A

In Swift this is a paradigm used to describe the blueprint of functions and properties that a conforming object needs to adhere to.

protocol Vehicle {
  var wheels: Int { get }
  var color: UIColor { set get }
  func drive(speed: Int)
}
struct Bike: Vehicle {
  let wheels = 2
  var color = UIColor.systemGray
  func drive(speed: Int) {
    print("current speed is \(speed)")
  }
}

let bike = Bike()

bike.drive(speed: 23) // current speed is 23

18
Q

What is dependency injection?

A

Dependency Injection is used to pass all required properties and data over to an object. This is better done through the use on an initializer as the object can fully encapsulate its properties.

19
Q

What framework is used for writing Unit Test in iOS ?

A

XCTest

20
Q

What is a Singleton?

A

A singleton is making use of one instance of a class throughout the life of the launch of an application. One of the main pillars of singleton is the use of marking initializers private so accidental creation of multiple instances is prohibited.

Singletons are used throughout iOS in places like UserDefaults.standard, FileManager.default and UIApplication.shared.

21
Q

Is URLSession part of Foundation or UIKit?

A

URLSession is part of the Foundation framework.

22
Q

What’s the difference between a compile time error and a runtime error?

A

Compile time errors occurs during the writing phase of your code. Runtime errors occurs during the launch and actual use of the application.

23
Q

Is Index out of range error on an array an compile-time error or a runtime error?

A

Index out of range is a runtime error.

24
Q

What’s the difference between Structs and Classes?

A

Structs are passed-by value (value-types) meaning copies of the objects are passed around thereby making the objects immutable by default. Classes are reference types and their state is easily mutated as objects that have the same reference can make changes at will.

25
Q

What is Type Annotation?

A

Type annotation is explicitly marking the data type of a variable or constant upon initialization.

26
Q

What is Type Inference?

A

Type inference is allowing the Swift compiler to determine the data type.

27
Q

Is NSString a class or a struct?

A

NSString is an objective-c API and is a class. With interoperability we can easily bridge between Swift String and NSString.

28
Q

What’s the difference between frames and bounds?

A

The frame represents an object’s superview and it’s relationship in the coordinate space, whereas the bounds represents the objects own size and location.