iOS Flashcards

1
Q

What is ARC?

A

Automatic reference counting:

  • every instance in memory has a counter of the number of references to that instance.
  • When the counter hits 0, then that instance can be safely removed from memory
  • strong reference: when you have at least one pointer to an object
  • weak reference: when there are no pointers to an object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the main differences between classes and structs in Swift?

A

Struct is value type (Stack); Class is reference type (Heap – slower in performance).

When you copy a struct, you end up with two unique copies of the data.
When you copy a class, you end up with two references to one instance of the data

Structs do not support inheritance but can adhere to protocols. Classes support inheritance.

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

What is the delegation pattern?

A

Delegation allows you to have one object delegate a task to another object. For example, having a viewcontroller provide the datasource for a UITableView

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

Explain MVC

A

Architecture approach that separates code into models, views, and controllers. Controllers generally handle the logic and act as an interface between the model and the view. Problem is with “massive view controllers”. For example if your view required date formatting, where should that formatting occur? Frequently dumped into the controller.

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

Explain MVVM

A

Model-view-viewmodel. Architecture approach that is similar to MVC but creates a viewModel layer for handling view logic. For example if your view required date formatting, then the viewmodel would be a good place for the date formatting to occur. This allows for lean view controllers and plays nice with reactive architectures, can setup all bindings in VC and then all logic happens in VM.

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

Explain Dependency Injection and give an example

A

DI is a pattern where we provide an object’s dependencies to it, instead of having this object seek out it’s dependencies. For example, if our VC needs a network service, then we can pass that networking object into the VC’s init. Benefits of this include: easier for testing, cleaner code, and less tightly coupled code.

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

What are the benefits of dependency injection?

A
  • Better, more modular, testing
  • Less tightly coupled code
  • cleaner, easier to read
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a retain cycle? Give an example

A

A retain cycle occurs when two objects are holding references to each other. Common example is when you reference self inside of a closure.

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

How to identify a retain cycle?

A

Use instruments tool or memory debug graph in xcode

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

Describe the view lifecycle

A
  • viewDidLoad
  • viewWillAppear
  • viewWillLayoutSubviews
  • viewDidLayoutSubviews
  • viewDidAppear
  • viewWillDisappear
  • viewDidDisappear
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe the different access modifiers

A
  • public: can be called from any place within the module, or if the module is imported
  • internal: can be called only from within the moudle
  • file-private: can only be used within the file
  • private: can only be used within the same declaration and it’s extensions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When do we need an escaping closure?

A

If the closure is called after the function is returned, like after a networking delay, it must be marked as escaping

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

How would you improve UITableView scroll performance?

A
  • cell reuse
  • pre-render the cell height
  • cache images if possible
  • avoid shadows, use rasterize to improve shadow performance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is GCD?

A

low level api that allows users to run concurrent tasks, multithreading

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

Describe the different app states

A
  • Not Running: The app has not been launched or it has been terminated.
  • Inactive: The app is in the foreground but not receiving events, Ex. User has locked the device with the app active.
  • Active: The normal state of “In Use” for an app.
  • Background: The app is no longer on-screen, but still executing the code.
  • Suspended: The app is still resident in memory but is not executing code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If the user were to scroll really quickly, what would happen in the cells? Would they all show the correct image?

A
  • In prepareForReuse set cell’s image to nil
  • Might mention cancelling the network fetch
  • Might mention checking the image url that was fetched against the one the cell is expecting to prevent populating the cell with the wrong image
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Which thread must UI work be done on?

A

Main

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

What is a memory leak?

A

When an object that should be deallocated, has not been deallocated

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

How would you describe the swift language?

A

type-safe, can be used for scripting, or programming, across mobile, desktop, and sever. Can leverage protocols, and reactive functional paradigms

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

When would you use a DispatchGroup?

A

Dispatch groups are good for when we want to perform more than 1 time consuming operations before proceeding with some logic. For example, if we need to make multiple different requests to build a model, we can group those requests to simplify the order of events

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

What are locks in regards to GCD?

A

Locks prevent race conditions by requiring that a thread have a lock to access a piece of code. Threads w/o the lock cannot access that code.

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

What design patterns are you familiar with?

A
  • MVC
  • MVVM
  • Coordinator
  • Delegate pattern
  • Factory pattern
  • VIPER
  • dependency injection / inversion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Explain the diff between strong and weak pointers

A
  • strong reference increases the retain count of an object by 1
  • weak reference is a pointer that does not increase the retain count

https://www.appypie.com/weak-vs-strong-references-swift

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

Why do we use weak references? Give some examples

A
  • prevent retain cycles where two objects are pointing at each other
  • examples:
    • delegate properties: w/ tableview, we don’t want a tableVC to hold a strong reference to it’s datasource, otherwise we would get a retain cycle
    • closures: closures can outlive the scope that they’re declared in. Use [weak self]

https://www.appypie.com/weak-vs-strong-references-swift

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

Explain the difference between weak and unowned?

A
  • Weak: You use weak to create a weak reference to an object. This will automatically make that instance optional inside the closure.
  • Unowned: Same as weak, except that the referenced instance is not an optional. You use this when you’re certain the instance cannot become nil.
    https: //www.appypie.com/weak-vs-strong-references-swift
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

When are instances deallocated in ARC?

A

When the retain count hits 0

https://www.appypie.com/weak-vs-strong-references-swift

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

How would you manage a reference to self in a closure?

A

Use a capture list such as [weak self]

https://www.appypie.com/weak-vs-strong-references-swift

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

Are structs stored in the stack or heap?

A

Structs are stored in the stack

https://stackoverflow.com/questions/54549033/are-swift-structs-always-stored-in-the-stack

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

Are classes stored in the stack or heap?

A

Classes are stored in the heap

https://stackoverflow.com/questions/54549033/are-swift-structs-always-stored-in-the-stack

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

Why would someone choose a struct over a class?

A
  • structs are copiable and potentially safer than having many pointers modifying it. important when passing around data
  • less likely to have retain cycles bc struct is value type not reference
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the memory debug graph?

A

Allows user to identify memory leaks inside of xcode. Good for spotting retain cycles

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

Define: concurrency

A

Doing multiple tasks at once

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

Define: thread

A

a “lane” for tasks to occur

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

What kind of operations should happen on the main thread?

A

UI / animations

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

What APIs can you use to manage concurrency and threading?

A

NSOperationQueue and GrandCentralDispatch

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

Name two types of queues

A
  • serial queue

- concurrent queue

37
Q

Explain a serial queue

A
  • Tasks are executed in order, the second task does not begin until the first task is completed
38
Q

Explain how a concurrent queue works

A

Tasks begin in order, but can complete out of order

39
Q

What are the benefits of using a serial queue?

A
  • predictable execution order

- prevents race conditions

40
Q

What are the benefits of a concurrent queue?

A
  • faster than serial queue
41
Q

Is the main thread a serial or concurrent queue?

A

serial

42
Q

Is a background thread serial or concurrent?

A

concurrent

43
Q

What’s the problem with concurrent queues?

A

Race conditions, unpredictable completions

44
Q

Describe the Public access modifier

A

Makes an object available to any other object in the module, or if the module is imported

45
Q

Describe the Private access modifier

A

Makes an object available only to the enclosing declaration and it’s extensions

46
Q

Describe the file-private access modifier

A

Restricts object access to a file

47
Q

When would you use a private access modifier? Give an example

A

Use a private access modifier when you want to hide implementation details. For example, you have a price calculator, instead of exposing all of the calculations to other classes, maybe you set the calculations to private but set the getPrice() func to be public

48
Q

Describe the internal access modifier?

A

Object can be used within source module, but not outside

49
Q

What is a closure?

A

a block of code that you can store in a variable and pass around

50
Q

What is an escaping closure?

A

a closure that completes after the function that it was passed to returns. For example, dataTaskWithURL

51
Q

What is a non-escaping closure?

A

A closure that’s called within the function its passed to, before the function returns

52
Q

Explain the difference between bounds and frame?

A

frame: position of a view to it’s superview, top-left of view is x,y
bounds: positions inside of the view, top-left of view is 0,0

53
Q

What does a protocol extension allow us to do?

A

Allows us to create some optional protocol requirements

54
Q

Explain the diff between == and ===

A
=== compares references pointers
== compares equatable values
55
Q

What is the nil coalescing operator?

A

provides a default value when unwrapping: ??

56
Q

What are your options for persisting data on iOS?

A
  • coredata
  • user defaults
  • keychain
  • icloud
  • sqlite
57
Q

How would you implement debouncing on a search bar that returns live results?

A
  • rxswift: cancel old requests when a new request happens
  • could delay firing the request for a second
  • GCD: DispatchWorkItem.cancel()
58
Q

What is a race condition?

A

When more than one thread attempts to modify a value at the same time

59
Q

What is a deadlock?

A

two threads are waiting for each other to finish, inifinitely

60
Q

Give an example of when you would use generics

A

You could use generics to allow a function to accept multiple types as an input

61
Q

Ways that a function can indicate an error

A
  • throw
  • return result
  • return an optional
62
Q

CompactMap vs Map vs FlatMap

A
  • map: converts A to B
  • compactMap: same as map but excludes nil, optionals
  • flatMap: flattens an array of arrays into one array
63
Q

What does Reduce do in Swift?

A

Combines elements of an array in a specified manner, ie get the total letter count of all strings in an array

64
Q

What do you like about Swift?

A
  • strong type safety
  • readability, verbose
  • functional reactive programming, swiftUI
65
Q

What dont you like about Swift?

A

sometimes i feel overhwelmed with the velocity that new features are released, and it’s unclear to me what the future of Swift looks like. For example, Swift now has both the Combine framework and the async/await api to handle asynchronous events. To me those feel like competing tools and I guess i worry about which tool to invest myself in

66
Q

What do you like about iOS?

A

“It just works”

67
Q

What dont you like about iOS?

A

Xcode bugs. One version broke swiping back navigation tabs

68
Q

Why is Swift considered strongly typed?

A

The compiler enforces typing. You cant add an Int to a String array

69
Q

Define method swizzling

A

Change the implementation of a function at runtime so it behaves differently. Literally swapping function signatures.

70
Q

Define the “not running” app state

A

The app has not been launched or it has been terminated.

71
Q

Define the “Inactive” app state

A

The app is in the foreground but not receiving events, Ex. User has locked the device with the app active.

72
Q

Define the “active” app state

A

The normal state of “In Use” for an app.

73
Q

Define the “Background” app state

A

The app is no longer on-screen, but still executing the code.

74
Q

Define the “Suspended” app state

A

The app is still resident in memory but is not executing code. ie answering a phone call

75
Q

What state is your app in when interrupted to answer a phone call?

A

Suspended

76
Q

What state describes the app when it’s in the foreground but there are no events firing?

A

inactive

77
Q

What state describes when the app is terminated?

A

not running

78
Q

What state describes when the app is running but not on screeN?

A

background

79
Q

Difference between Delegate pattern and KVO / NSNotificationCenter

A

Delegate pattern is 1:1 relationship, KVO+NSNotificationCenter are 1:Many

80
Q

What’s the difference between CoreData and SQLite database?

A

CoreData is object graph, SQLite is relational db

81
Q

What db pattern does CoreData use? ie relational

A

object graph

82
Q

What is MVVM good for?

A
  • preventing massive VC
  • separation of concerns
  • good for testing
  • good for reactive paradigms
  • good for dependency injection
83
Q

Is Swift an object-oriented language or a functional language?

A

Swift is a hybrid language that supports both paradigms.

84
Q

Are closures value or reference types?

A

Closures are reference types. If a closure is assigned to a variable and the variable is copied into another variable, a reference to the same closure and its capture list is also copied.

85
Q

What considerations do you need when writing a UITableViewController which shows images downloaded from a remote server?

A
  • Only download the image when the cell is scrolled into view, i.e. when cellForRowAtIndexPath is called.
  • Downloading the image asynchronously on a background thread so as not to block the UI so the user can keep scrolling.
  • cache the images
  • use placeholder images
  • clear images in prepareForReuse
86
Q

What is a protocol? Give an example?

A

A protocol is a tool for requiring an object to implement a set of functions. A common use is requiring a viewcontroller to conform to UITableViewDelegate

87
Q

What is @escaping?

A

Tells the compiler that its ok for this closure to outlive the rest of the function

88
Q

Define closures

A

Closures are self contained blocks of code that can be stored and passed