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

what is a deadlock

A
  • when two or sometimes more tasks wait for the other to finish, and neither ever does
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

explain swizzling

A
  • Method Swizzling is a well-known practice in Objective-C and in other languages that support dynamic method dispatching.
  • Through swizzling, the implementation of a method can be replaced with a different one at runtime, by changing the mapping between a specific #selector(method) and the function that contains its implementation.
  • The class containing the methods to be swizzled must extend NSObject
  • The methods you want to swizzle must have the dynamic attribute
17
Q

escaping vs non-escaping closures

A
  • allows the the closure to outlive the function

- allows you to store the result in a global property, for example

18
Q

explain dependency injection and it’s different types

A
  • giving an object its instance variables
  • useful in testing to insert mock data
  • constructor/initializer injection: a dependency injected upon object initialization
  • property injection: injecting a dependency into a property (eg, view.title = “Title”)
  • method injection: injecting a dependency in the parameters of a method
19
Q

what are functional methods

A

filter, map, compactMap, reduce

20
Q

filter vs map

A
  • Map, we pass in a function that returns a value for each element in an array. The return value of this function represents what an element becomes in our new array.
  • Filter, we pass in a function that returns either true or false for each element. If the function that we pass returns true for a given element, then the element is included in the final array.
21
Q

compare frame vs bounds

A
  • The bounds of a UIView is the rectangle, expressed as a location (x,y) and size (width, height) relative to its own coordinate system (0,0).
  • The frame of a UIView is the rectangle, expressed as a location (x,y) and size (width, height) relative to the superview it is contained within.
22
Q

what are the advantages of Swift

A
  • Optional Types, which make applications crash-resistant
  • Built-in error handling
  • Closures
  • Much faster compared to other languages
  • Type-safe language
  • Supports pattern matching
23
Q

what are escaping vs non-escaping closures

A
  • non-escaping is the default behavior
  • non-escaping dereferences the properties when it completes
  • escaping allows the the closure to outlive the function
  • escaping allows you to store the result in a global property, for example
  • escaping requires a weak reference to self ([weak self] in, and guard let self = self to avoid using optional chaining)
24
Q

what is a memory leak

A
  • when an amount of allocated space in memory cannot be deallocated due to retain cycles
25
Q

what is ARC

A
  • Apple’s way of handling memory management
  • It keeps track of class instances and it decides when it is safe to deallocate the class instances it monitors. It does this by counting the references of each class instance.
  • ensures a class instance isn’t deallocated as long as a property, constant, or variable keeps a strong reference to the class instance.
26
Q

what is nil coalescing

A
let name: String? = nil
let unwrappedName = name ?? "Anonymous"
27
Q

what is a ternary operator

A
let score = 88
let result = score > 85 ? "Pass" : "Fail"
28
Q

explain dependency injection and it’s different types

A
  • giving an object its instance variables
  • allows you to pass mock dependencies for testing
  • initializer injection: passing dependencies when initializing an object
  • property injection: pass a dependency to a property on an object (eg, vc.manager = Manager())
  • method injection: requires the dependency in the method parameters
29
Q

explain filter

A
  • use filter to loop over a collection and return an Array containing only those elements that match an include condition.
30
Q

explain map, flatMap, and compactMap

A
  • use map to loop over a collection and apply the same operation to each element in the collection
  • use compactMap to unwrap optionals and remove nils
  • use flatMap to flatten, or merge, multiple sequences into a single sequence (eg, 3 arrays into 1 array)
31
Q

explain reduce

A
  • use reduce to combine all items in a collection to create a single new value
  • it takes two values: an initial value, and the combine closure or operator
32
Q

explain value vs reference types

A
  • in Swift, value types are structs, and reference types are classes
  • value types pass a copy of a property
  • reference types pass a reference to an instance in memory
  • structs have memberwise initializers
  • classes can use inheritance
33
Q

what are extensions

A
  • can be used to extend an existing type, even if you don’t have access to the original type - such as Date, or String
  • cannot hold a property
34
Q

compare guard let and if let

A
  • guard let is designed to exit the current function, loop, or condition if the check fails, so any values you unwrap using it will stay around after the check
  • guard let is when you’re fairly certain you’ll be on the happy path
  • if let to unwrap optional if you’re not sure if the value(s) will exist
  • if let does not require a return statement
35
Q

compare sync and async operations

A
  • Synchronous: waits until the task have completed

- Asynchronous: completes a task in the background and can notify you when complete

36
Q

explain defer

A
  • provides a block of code that will be executed in the case when execution is leaving the current scop
37
Q

explain GCD

A
  • Dispatch Queues, A dispatch queue is responsible for executing a task in the first-in, first-out order.
  • Serial Dispatch Queue A serial dispatch queue runs tasks one at a time.
  • Concurrent Dispatch Queue A concurrent dispatch queue runs as many tasks as it can without waiting for the started tasks to finish.
  • Main Dispatch Queue A globally available serial queue that executes tasks on the application’s main thread.
38
Q

explain capture lists

A
  • enable us to customize how a given closure captures any of the objects or values that it refers to
  • ie, ‘[weak self] user in’
39
Q

discuss Swift access control

A
  • Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.
  • Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
  • File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.
  • Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.