0% found this document useful (0 votes)
12 views

Swift-Interview-Questions

The document provides an overview of key concepts in Swift, including memory management, the differences between classes and structs, and the iOS app lifecycle. It explains memory leaks, retain cycles, and the roles of weak and unowned references, as well as outlining the use of protocols and closures. Additionally, it covers Grand Central Dispatch (GCD) for managing concurrent tasks and the characteristics of various data structures like arrays, sets, and dictionaries.

Uploaded by

Chung Duc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Swift-Interview-Questions

The document provides an overview of key concepts in Swift, including memory management, the differences between classes and structs, and the iOS app lifecycle. It explains memory leaks, retain cycles, and the roles of weak and unowned references, as well as outlining the use of protocols and closures. Additionally, it covers Grand Central Dispatch (GCD) for managing concurrent tasks and the characteristics of various data structures like arrays, sets, and dictionaries.

Uploaded by

Chung Duc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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 and Closure

– 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.

iOS App Lifecycle

. 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.

Automatic Reference Counting

– 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

– DispatchQueue - An object that manages the execution of tasks serially or concurrently on


your app's main thread or on a background thread.
– DispatchGroup - A group of tasks that you monitor as a single unit.
– Grand Central Dispatch is a technology that is designed to make the execution of tasks on
multicore hardware performant and straightforward. It can do this because it operates at the
system level. Your application operates in a sandbox, which means that it is unaware of other
processes running on the system at the same time. Because Grand Central Dispatch operates
at the system level, it has an accurate view of the processes that are running and the
resources that are available. Grand Central Dispatch allows applications to schedule work for
execution through an easy to use API.
– A lazy variable‘s initial value is computed when calling it for the first time. Lazy variables can
be used to optimize code by not doing unnecessary work before needed.
– An autoclosure wraps a function argument into a closure. When an autoclosure is called, it
returns the value of the expression wrapped inside.

● Arrays: An array is an ordered collection of values.


● Sets: A set is an unordered collection of values.
● Dictionaries: A Dictionary is an unordered collection of key-value pairs.

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.

You might also like