Swift Flashcards
1
Q
what is a strong reference
A
- default behavior
- incremements ARC
- allows it to be captured by ARC, keeping it in memory as long as there is a reference to it
2
Q
what is a weak reference
A
- not taken into account by ARC
- they do not keep a reference to the instance they are referencing
- can be set to nil, so always an optional
- must be unwrapped to use
- important to use when there may be retain cycles
3
Q
what is an unowned reference
A
- not taken into account by ARC
- they do not keep a reference to the instance they are referencing
- always expected to have a value
- can directly access the value
4
Q
compare weak vs unowned
A
- use weak when it’s valid for the reference to be nil at some point
- use unowned when you know the reference will never be nil once set during initialization
5
Q
what is a lazy reference
A
- only created when they are first needed
- must always be a var
- the initial value is not calculated the first time it is used
- can’t be used with computed properties
- are not initialised atomically and so is not thread safe.
6
Q
what is an optional
A
- a property that can be nil, ie, can handle the absence of a value
- in Swift, an enum that has two cases: .none and .some
7
Q
what is a retain cycle
A
- a retain cycle occurs when two or more objects hold strong references to each other.
- as a result these objects retain each other in memory because their retain count would never decrement to 0, which would prevent deinit from ever being called and memory from being freed
8
Q
what are generics
A
- create code that does not get specific about underlying data types
- allow us to know what type it is going to contain
- provides optimization for our code
9
Q
what are protocols
A
- a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.
- it can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.
- can be extended to implement some of these requirements, or to implement additional functionality
10
Q
what are protocol extensions
A
- provide method, initializer, subscript, and computed property implementations to conforming types.
- This allows you to define behavior on protocols themselves, rather than in each type’s individual conformance or in a global function
11
Q
what are DispatchGroups
A
- allows for aggregate synchronization of work.
- We can use them to submit multiple different work items and track when they all complete, even though they might run on different queues.
- This behavior can be helpful when progress can’t be made until all of the specified tasks are complete.
12
Q
what is a closure
A
- self-contained blocks of functionality that can be passed around and used in your code
- can capture and store references to any constants and variables from the context in which they are defined
13
Q
what is concurrency and multithreading
A
- Process, An instance of an executing app
- Thread, Path of execution for code
- Multithreading, Multiple threads or multiple paths of execution running at the same time.
- Concurrency, Execute multiple tasks at the same time in a scalable manner.
- Queues, Queues are lightweight data structures that manage objects in the order of First-in, First-out (FIFO).
- Synchronous vs Asynchronous tasks
14
Q
what is a race condition
A
- occurs when two or more threads can access shared data and they try to change it at the same time
15
Q
what is a deadlock
A
- when two or sometimes more tasks wait for the other to finish, and neither ever does