Swift-Interview-Questions
Swift-Interview-Questions
Memory Leak
– Memory leak is a portion of memory that is occupied forever and never used again. It is
garbage that takes space and causes problems.
– When an object has a strong association to another object, it is retaining it. When I say object
I am talking about Reference Types, Classes basically.
– Struct and Enums are Value Types. It is not possible to create retain cycles with value types
only. When capturing and storing value types (structs and enums), there is no such thing as
references. Values are copied, rather than referenced, although values can hold references to
objects.
– When an object references a second one, it owns it. The second object will stay alive until it is
released. This is known as a Strong Reference. Only when you set the property to nil will the
second object be destroyed.
– Retain cycles are broken when one of the references in the cycle is weak or unowned. The
cycle must exist because it is required by the nature of the associations we are coding. The
problem is that all the associations cannot be strong. One of them must be weak.
– Weak: A variable can optionally not take ownership of an object it references to. A weak
reference is when a variable does not take ownership of an object. A weak reference can be
nil.
– Unowned: Like weak references, an unowned reference does not keep a strong hold on the
instance it refers to. Unlike a weak reference, however, an unowned reference is assumed to
always have a value. Because of this, an unowned reference is always defined as a non-
optional type. An unowned reference cannot be nil.
Struct Vs Class
– Classes can inherit from another class, like you inherit from UIViewController to create your
own view controller subclass.
– Classes can be deinitialized, i.e. you can invoke a deinit() function before the class is
–
destroyed
– Classes are reference types and structs are value types
– Struct do not support typecasting, but Class do.
– Value Type: When you copy a value type, each instance keeps a unique copy of the data. If
you change one instance, the other doesn’t change too.
– Reference Type: When you copy a reference type, each instance shares the data. The
reference itself is copied, but not the data it references. When you change one, the other
changes too.
Frame vs Bounds
– Frame refers to its coordinates relative to its parent’s space
– Bounds refers to its coordinates relative to its own space
– If you create a view at X:0, Y:0, width:100, height:100, its frame and bounds are the same.
– If you move that view to X:100, its frame will reflect that change but its bounds will not.
– Protocol is a blueprint for methods, properties, and other required functionality. Protocols are
declared after struct, enumeration, and classes. We declare multiple or single functions in it.
It is a delegate method. Protocol are like Interface in Java. They doesn’t have actual
implementation.
– Closure is used to pass around and used in our code for callbacks. or we can say that it is a
self-contained block of functionality.
. Not Running: the app is considered to be in a Not Running state when it is not yet launched
or terminated by the system or user.
. Inactive: the app is in an inactive state when it is in the foreground but receiving events. In
other words, we can say that it acts like a bridge state in which the app remains briefly when
it transitions to a different state.
. Active: it is a normal mode for the app when it is in the foreground state and receiving all the
user events.
. Background: the app transitions into the background state when the user taps on the home
screen while using the application, or it requires some extra execution time. When the app is
about to be suspended, then also transitions into this state for a small amount of time. In this
state, the app remains in the background and executes the code.
. Suspended: in this state, the app remains in the background and doesn't execute the code.
The app is automatically moved to this state. In this state, the app remains in memory.
However, the foreground apps are always given priority over suspended apps and can be
purged any time without notice.
– Every time you create a new instance of a class, ARC allocates a chunk of memory to store
information about that instance. This memory holds information about the type of the
instance, together with the values of any stored properties associated with that instance.
– Additionally, when an instance is no longer needed, ARC frees up the memory used by that
instance so that the memory can be used for other purposes instead. This ensures that class
instances don’t take up space in memory when they’re no longer needed.
Others
GCD (Grand Central Dispatch) is a low-level API for controlling several operations at the same time.
This procedure is used to handle numerous jobs at once. The most relevant API for multitasking
with Async and Sync programming in iOS is Grand Central Dispatch (GCD).
Dispatch Queue : The task is managed in FIFO (First In First Out) order by the Dispatch Queue.
Dispatch queues are thread-safe because they can be accessed by several threads at the same
time.
Concurrent : This process has started numerous tasks at the same time but does not know when
they will finish. It can be completed in whatever sequence you want. They perform one or more
things concurrently at the same time. The work is finished in the order in which it was queued, not
in the order in which it was completed.
Serial : A single task will be executed at a time. It is possible to use it to synchronize access to a
certain resource.
Sync : After the work is completed, a synchronous function returns control to the caller.
Asynchronous : An asynchronous function returns instantly after ordering a job to begin but does
not wait for it to finish.