Set_5 Flashcards
What is Memory Management and ARC?
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.”
What is MVC & Other Design Patterns You Use?
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).
What is the difference between value types and reference types?
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.
What is an optional in Swift and what problem do optionals solve?
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.
What are the main differences between Structures and Classes?
Classes support inheritance, structures don’t. Classes are reference types, structures are value types.
What are the various ways to unwrap an optional? How do they rate in terms of safety?
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
What is auto-layout?
Auto layout dynamically calculates the size and position of all the views in your view hierarchy based on constraints placed on those views.
What are Higher Order Functions in Swift?
Sort: Sorting arrays
Map: Transform array contents
Filter: Filter array based upon some criteria
Reduce: Reduce the values in collection to a single value
What is Lazy Loading?
Lazy loading means the calculation of the property’s value will not occur until the first time it is needed.
What is CoreData?
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.
Differences between Concurrent and Serial Queues?
Concurrent: multiple at the same time
Serial: first come first served, first in first out, ordered
When would you use Core Data over NSUserDefault?
NSUserDefault is typically used to store small bits of data (settings, preferences, etc.). Core Data is used to store a large list of elements.
What is Singleton Pattern?
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.
What is the delegation pattern?
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.
What is MVC?
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.