iOS Flashcards
What is ARC?
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
What are the main differences between classes and structs in Swift?
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.
What is the delegation pattern?
Delegation allows you to have one object delegate a task to another object. For example, having a viewcontroller provide the datasource for a UITableView
Explain MVC
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.
Explain MVVM
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.
Explain Dependency Injection and give an example
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.
What are the benefits of dependency injection?
- Better, more modular, testing
- Less tightly coupled code
- cleaner, easier to read
What is a retain cycle? Give an example
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 to identify a retain cycle?
Use instruments tool or memory debug graph in xcode
Describe the view lifecycle
- viewDidLoad
- viewWillAppear
- viewWillLayoutSubviews
- viewDidLayoutSubviews
- viewDidAppear
- viewWillDisappear
- viewDidDisappear
Describe the different access modifiers
- 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
When do we need an escaping closure?
If the closure is called after the function is returned, like after a networking delay, it must be marked as escaping
How would you improve UITableView scroll performance?
- cell reuse
- pre-render the cell height
- cache images if possible
- avoid shadows, use rasterize to improve shadow performance
What is GCD?
low level api that allows users to run concurrent tasks, multithreading
Describe the different app states
- 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.
If the user were to scroll really quickly, what would happen in the cells? Would they all show the correct image?
- 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
Which thread must UI work be done on?
Main
What is a memory leak?
When an object that should be deallocated, has not been deallocated
How would you describe the swift language?
type-safe, can be used for scripting, or programming, across mobile, desktop, and sever. Can leverage protocols, and reactive functional paradigms
When would you use a DispatchGroup?
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
What are locks in regards to GCD?
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.
What design patterns are you familiar with?
- MVC
- MVVM
- Coordinator
- Delegate pattern
- Factory pattern
- VIPER
- dependency injection / inversion
Explain the diff between strong and weak pointers
- 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
Why do we use weak references? Give some examples
- 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
Explain the difference between weak and unowned?
- 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
When are instances deallocated in ARC?
When the retain count hits 0
https://www.appypie.com/weak-vs-strong-references-swift
How would you manage a reference to self in a closure?
Use a capture list such as [weak self]
https://www.appypie.com/weak-vs-strong-references-swift
Are structs stored in the stack or heap?
Structs are stored in the stack
https://stackoverflow.com/questions/54549033/are-swift-structs-always-stored-in-the-stack
Are classes stored in the stack or heap?
Classes are stored in the heap
https://stackoverflow.com/questions/54549033/are-swift-structs-always-stored-in-the-stack
Why would someone choose a struct over a class?
- 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
What is the memory debug graph?
Allows user to identify memory leaks inside of xcode. Good for spotting retain cycles
Define: concurrency
Doing multiple tasks at once
Define: thread
a “lane” for tasks to occur
What kind of operations should happen on the main thread?
UI / animations
What APIs can you use to manage concurrency and threading?
NSOperationQueue and GrandCentralDispatch