Set_5 Flashcards

1
Q

What is Memory Management and ARC?

A

A program needs to allocate and deallocate memory as needed. Swift does that automatically. Swift does not use a garbage collector which is a common tool. ARC is Automatic Reference Counting introduced for Obj-C.
Everything is automatically managed, but you need to know ARC to avoid memory leaks.
Retain Cycles?
Retain cycles happens when a reference is pointing to another reference so it will never be removed from the heap.
This mostly happens in classes and closures. Closures live in memory, so when you use Self which is a reference you need to make sure that you solve the retain cycle.
Weak, unowned, strong?
Unless you specify otherwise, all Swift properties are strong, which means they will not be removed from RAM until whatever owns them is removed from RAM.
Weak on the other hand is there when you want to say “I want to be able to reference this variable, but I don’t mind if it goes away, so I don’t want to own it.” This might seem strange: after all, where’s the point in having a reference to a variable that might not be there?
Unowned means “don’t mind this one, I will make sure it is removed from memory.”

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

What is MVC & Other Design Patterns You Use?

A

Model-View-Controller is a design pattern based on three job categories: model, view or controller.
Model: Models are responsible for storing data and making it available to other objects.
View: Views are the visual elements of an application. What you see on screen.
Controllers: Controllers perform the logic necessary connect your views and models. They process events.
You may use different kinds of design patterns when you are developing your apps, but here are the most common ones.
Singleton: The Singleton design pattern ensures that only one instance exists for a given class
Singleton Plus: you can create another object. Doesn’t force you to use the shared one.
Facade: The Facade design pattern provides a single interface to a complex subsystem. Let’s say you have NetworkManager class where you make HTTP requests and you have JSON response. With using Facade, you can keep your NetworkManager just focused on networking.
Decorator: The Decorator pattern dynamically adds behaviors and responsibilities to an object without modifying its code.
Adapter: An Adapter allows classes with incompatible interfaces to work together. It wraps itself around an object and exposes a standard interface to interact with that object.
Observer: In the Observer pattern, one object notifies other objects of any state changes. Cocoa implements the observer pattern in two ways: Notifications and Key-Value Observing (KVO).

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

What is the difference between value types and reference types?

A

The major difference is that value types are copied when they are passed around, whereas reference types share a single copy of the referenced information.

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

What is an optional in Swift and what problem do optionals solve?

A

An optional is used to let a variable of any type represent the lack of value. An optional variable can hold either a value or nil any time.

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

What are the main differences between Structures and Classes?

A

Classes support inheritance, structures don’t. Classes are reference types, structures are value types.

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

What are the various ways to unwrap an optional? How do they rate in terms of safety?

A
forced unwrapping ! operator — unsafe
implicitly unwrapped variable declaration — unsafe in many cases
optional binding — safe
optional chaining — safe
nil coalescing operator — safe
guard statement — safe
optional pattern — safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is auto-layout?

A

Auto layout dynamically calculates the size and position of all the views in your view hierarchy based on constraints placed on those views.

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

What are Higher Order Functions in Swift?

A

Sort: Sorting arrays
Map: Transform array contents
Filter: Filter array based upon some criteria
Reduce: Reduce the values in collection to a single value

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

What is Lazy Loading?

A

Lazy loading means the calculation of the property’s value will not occur until the first time it is needed.

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

What is CoreData?

A

Core Data is a framework for managing an object graph. An object graph is nothing more than a collection of interconnected objects. The framework excels at managing complex object graphs.

Core Data is a framework that is used to manage model layer objects. It has the ability to persist object graphs to a persistent store. Data is organized into relational entity-attribute model.

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

Differences between Concurrent and Serial Queues?

A

Concurrent: multiple at the same time
Serial: first come first served, first in first out, ordered

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

When would you use Core Data over NSUserDefault?

A

NSUserDefault is typically used to store small bits of data (settings, preferences, etc.). Core Data is used to store a large list of elements.

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

What is Singleton Pattern?

A

The Singleton design pattern ensures that only one instance exists for a given class and that there’s a global access point to that instance. It usually uses lazy loading to create the single instance when it’s needed the first time.

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

What is the delegation pattern?

A

The delegation pattern is a powerful pattern used in building iOS applications. The basic idea is that one object will act on another object’s behalf or in coordination with another object. The delegating object typically keeps a reference to the other object (delegate) and sends a message to it at the appropriate time. It is important to note that they have a one to one relationship.

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

What is MVC?

A

MVC stands for Model-View-Controller. It is a software architecture pattern for implementing user interfaces.

MVC consists of three layers: the model, the view, and the controller.

The model layer is typically where the data resides (persistence, model objects, etc)
The view layer is typically where all the UI interface lies. Things like displaying buttons and numbers belong in the view layer. The view layer does not know anything about the model layer and vice versa.
The controller (view controller) is the layer that integrates the view layer and the model layer together.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is MVVM?

A

MVVM stands for Model-View-ViewModel. It is a software architecture pattern for implementing user interfaces.

MVVM is an augmented version of MVC where the presentation logic is moved out of the controller and into the view model. The view model is responsible for handling most, if not all, of the view’s display logic.

A common occurence in MVC is where you have a massive-view-controller (some joke this is what MVC stands for). In order to shrink the size of your view controller and make the logic and readibility of your code easier to follow along, the MVVM will be used.

17
Q

What is the difference between a class and an object?

A

In the simplest sense, a class is a blueprint for an object. It describes the properties and behaviors common to any particular type of object. An object, on the other hand, is an instance of a class.

18
Q

What is JSON? What are the pros and cons?

A

JSON stands for JavaScript Object Notation. According to wiki, it is a file format that uses human-readable text to transmite data objects consisting of attribute-value pairs and array data types.

Pros:

It is lighter than XML meaning that it can represent the same data in XML in fewer bytes. This makes network transmissions and read/writes faster
Since it is native to JavaScript, computationally-expensive XSL tranformations are not needed in order to extract data
Cons:

Not as widespread as XML
Data is not readily streamable and has to be broken up into individual objects
Can’t use comments

19
Q

What is the difference between not-running, inactive, active, background and suspended execution states?

A

Not-running state occurs when the app either has not be launched or was running but was terminated by the system.
Inactive state occurs where the app runs in the foreground but is currently not receiving events. (It may be executing other code though). This state is typically brief as apps transitions to other states.
Active state is where the app is running in the foreground and receiving events. This is the normal mode for foreground apps.
Background state occurs when the app is in the background and executing code. Apps typically enter this state on their way to being suspended. Apps that require extra execution time may remain in this screen longer. Apps being launched directly into the background enters this state instead of inactive state.
Suspended state is where the app is in the background but it is not executing code. Apps will remain in memory, but are removed by the system if low-memory condition occurs in order to make more space for foreground apps.

20
Q

What is KVO?

A

KVO stands for Key-Value Observing. It allows a controller or class to observe when a property value changes.

21
Q

What is memory management handled on iOS?

A

iOS uses something called ARC which stands for Automatic Reference Counting. When an object is said to have a strong reference to it, ARC increase its retain count by 1. When the retain count of an object reaches 0, the object will typically be deallocated if there are no more strong references to it. Unlike garbage collection, ARC does not handle reference cycles automatically.

22
Q

What is the difference between weak and strong?

A

First, objects are strong by default.

Strong means that the reference count will be increased and the reference to it will be maintained through the life of the object.
Weak, means that we are pointing to an object but not increasing its reference count. It’s often used when creating a parent child relationship. The parent has a strong reference to the child but the child only has a weak reference to the parent. (source)
Common instances of weak references are delegate properties and subview/controls of a view controller’s main view since those views are already strongly held by the main view. (source)

23
Q

What is a memory leak?

A

A memory leak commonly occurs when an object is allocated in such a way that when it is no longer in use or needed, it is not released. In iOS programming, you create certain objects with weak references in order to avoid a strong to strong relationship that creates a retain cycle and a memory leak.

24
Q

What is a retain cycle?

A

Retain cycles can occur when memory management is based on retain count. This typically occurs when two objects strongly reference each other. As a result, the retain count of either object will never reach zero and deallocated from memory (hence retaining each other).

25
Q

What is the difference between copy and retain?

A

Calling retain on an object will increase its retain count by one. When the retain count of an objective reaches 0, the object will be deallocated and released from memory.

When you retain an object, you share the same version with whoever passed the object to you. But when you copy an object, you do not share the same version of the object that was passed to you. Instead, a duplicate of that object is created with duplicated values.

26
Q

What is the difference between a stack vs a heap?

A

A stack is a region of memory where data is added or removed in a last-in-first-out (LIFO) order. According to Ates Goral, it is the memory set aside as scratch space for a thread of execution. Meanwhile the heap is memory set aside for dynamic allocation. Unlike the stack, you can allocate a block at any time and free it at anytime.

Note: In Objective-C, all objects are always allocated on the heap, or at least should be treated as if on the heap.

27
Q

What is the difference between public and open? Why is it important to have both?

A

Open access imposes limitations on class inheritance. Classes declared with open level access can be subclassed by modules they are defined in, modules that import the module in which the class is defined, and class members as well. While this sounds similar to the public access level defined in Swift 2, there is a small difference. In Swift 3, the meaning of public access level means that classes declared public can only be subclassed in the module they are defined in. This includes public class members which can be overridden by subclasses defined int he module they are defined in.

Some classes of libraries and frameworks are not designed to be subclasses. For example, in the Core Data framework, Apple states that some methods of NSManagedObject should not be overridden. To prevent any unexpected behavior that may result from overriding those methods, Apple declares those methods public rather than open. As a result, those methods are not marked as open for developers to override.

28
Q

What is the difference between a struct and a class?

A

The main difference to note is that structs are value types (stored on stack) while classes are reference types (stored on heap).

Classes have capabilities that structs do not:

Inheritance enables one class to inherit the characteristics of another. (source)
Type casting enables you to check and interpret the type of a class instance at runtime.
Deinitializers enable an instance of a class to free up any resources it has assigned.
Reference counting allows more than one reference to a class instance.
29
Q

What is the difference between synchronous and asynchronous task?

A

Synchronous tasks wait until the task has been completed while asynchronous tasks can run in the background and send a notification when the task is complete.

30
Q

What is GCD and how is it used?

A

GCD stands for Grand Central Dispatch. According to Ray Wenderlich, it offers the following benefits

Improving your app’s responsiveness by helping to defer computationally expensive tasks and run them in the background.
Providing an easier concurrency model than locks and threads and helps to avoid concurrency bugs.
Potentially optimize your code with higher performance primitives for common patterns such as singletons.
In other words, GCD provides and manages queues of tasks in the iOS app. This is one of the most commonly used API to manage concurrent code and execute operations asynchronously. Network calls are often performed on a background thread while things like UI updates are executed on the main thread.