iOS Interview Questions Answers
iOS Interview Questions Answers
Page 1 of 29
2- What is Typealias ?
A type alias allows you to provide a new name for an existing data type into your program. After a type alias is
declared, the aliased name can be used instead of the existing type throughout the program.
Page 2 of 29
Page 3 of 29
The key difference between Upcast and Downcast in iOS Swift is that upcasting from a derived to a base class can
be verified at compile-time and will never fail to compile and Downcasts, on the other hand, can fail to compile since
the precise class is not always known. It is possible that the UIView you have is a UITableView or a UIButton.
Page 4 of 29
By adding a label to the outer loop we can break out of both loops at once, like this :-
travel() {
print("I'm driving in my car”)
Page 5 of 29
Self - use in protocol and extension declarations to refer to the eventual type that will conform to the protocol.
self (Lower Case) - explicit reference to the current type or instance of the type in which it occurs
Page 6 of 29
Unowned Reference
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 de ned
as a non-optional type. (From Apple Docs
Page 7 of 29
fi
18- When to use a set rather than an array in Swift?
Sets are especially useful when you need to ensure that an item only appears once in the set. but not in order.
Array in order but can be duplicate items
file private it can be read anywhere in the same file it was declared – even outside the type.
private property can only be read inside the type that declared it, or inside extensions to that type that were
created in the same file.
Page 8 of 29
Page 9 of 29
23-What is the use of Hashable protocol?
If an object conforms to the Hashable protocol, it needs to have A Hash Value , The Hash Value can be used to
compare objects / uniquely identify the object.
You can compare objects in two ways
1. === function. This checks object references (can only be used with classes). It checks if the left object has the same
reference to the right object. Even if both objects have exactly the same property values BUT they do have a different
reference, it returns false.
2. == function (Equatable protocol). It checks if the objects are equal to each other based on the static func ==. You can
return the hashValue of the object. In that way, you can say objects are equal to each toher based on the properties, rather
than reference
Page 10 of 29
.
• Struct has free initializer for you , you don't have to declare initializer if you do free initializer will be overwritten by your
declared initializer
• Struct Don't have deinitializer
• Struct Cannot inherit from other struct
• Class is reference type :- When you make a copy of a reference type, the new variable refers to the same memory
location as the thing you are copying. This means that changing one will change the other since they both refer to the
same memory location. The sample code below could be taken as reference
(Note: - There’s more difference between struct and classes like struct stored in stack , classes instances stored in heap you can search
about it
Page 11 of 29
)
Page 12 of 29
It is var and not let because, the value is not initialized during the initialization process. It is calculated later on. That's why a
lazy stored property need to be a Variable and not a Constant
Page 13 of 29
fi
y
Page 14 of 29
Page 15 of 29
If you want a case of an enum to carry arbitrary value(s) of a prede ned type, use an Associated value
enum Error : ErrorType {
case Number(Int)
case Message(String)
}
Remember, Swift is a type-safe language, which means it won’t let you mix types. For example, you can’t add an integer to a string because it
doesn’t make any sense.
Page 16 of 29
fi
.
When user launches an app that is currently in background, the system moves app to the inactive state and then to the
active state.
40-What are the steps involved when app enter to foreground after device rebooted?
When user launches an app for the first time or after device reboot or after system terminate the app, the system
moves app to the active state.
Page 17 of 29
fi
42-When app is running but not receiving event. In which state app is in?
Inactive state.
43-How an iOS app responds to interrupts like SMS, Incoming Call, Calendar, etc.?
Application moves to the inactive state temporarily and it remains in this state until the user decides whether to accept
or ignore the interruption.
If the user ignores the interruption, the application is reactivated.
If the user accepts the interruption, the application moves into the suspended state.
1- ViewDidLoad - Called when you create the class and load from xib. Great for initial setup and one-time-only work.
2- ViewWillAppear - Called right before your view appears, good for hiding/showing fields or any operations that you
want to happen every time before the view is visible. Because you might be going back and forth between views, this
will be called every time your view is about to appear on the screen.
3- ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data
from an API.
Page 18 of 29
Protocol is a set of methods (either optional or required) that would be implemented by the class which conforms to
that protocol. While, delegate is the reference to that class which conforms to that protocol and will adhere to
implement methods defined in protocol.
47-is it possible that an iOS Application can have more than one window?
Yes it can have multiple windows. Just only one to be displayed at a time .
Page 19 of 29
You can think of delegates like a telephone call. You call up your buddy and specifically want to talk to them. You can
say something, and they can respond. You can talk until you hang up the phone. Delegates, in much the same way,
create a link between two objects, and you don't need to know what type the delegate will be, it simply has to
implement the protocol , <ONE TO ONE Communication> .
On the other hand, NSNotifications are like a radio station. They broadcast their message to whoever is willing to listen.
The radio station can't receive feedback from it's listeners (unless it has a telephone, or delegate). The listeners can
ignore the message, or they can do something with it. NSNotifications allow you to send a message to any objects, but
you won't have a link between them to communicate back and forth. If you need this communication, you should
probably implement a delegate. Otherwise, NSNotifications are simpler and easier to use, but may get you into trouble.
<ONE TO MANY Communication>
it’s Apple way of handling memory of objects for developers , it’s keeping count for each object of how many strong
references are pointing to that object . the object removed from memory when the count equal zero.
Page 20 of 29
fi
it’s Happening when two object got strong references from each others , so that they can’t be removed from memory
because arc keep tracking the strong references of each object and removing them when the count equal zero .
it can be fixed by making one of those objects contains a weak ref instead of strong reference.
we can detect memory leaks from the visual memory debugger or from profiling memory leaks.
51-What is GCD?
GCD is a library that provides a low level and object based API to run tasks concurrently while managing threads
behind the scenes. GCD abstracts away thread management code and moves it down to the system level, exposing a
light API to define tasks and execute them on an appropriate dispatch queue.
Page 21 of 29
fi
fi
In iOS, application main thread is a globally available serial queue. Main thread should be used for all views and
interface elements must not be blocked by long running tasks. Developer should avoid functions using main thread to
load data, images, etc.
The serial queue can run tasks one at a time and it needs to wait for the started tasks to finish.
The concurrent queue can run as many tasks as it can without waiting for the started tasks to finish.
Page 22 of 29
Quality of Service is the priority to perform task in GCD. If the task has higher quality of service than other it will be
handled before lower quality of service.
Quality of Services are ranked from highest to lowest –
1- User Interactive: Work that happens on the main thread, such as animations or drawing operations.
2- User Initiated: Work that the user kicks off and should yield immediate results. This work must be completed for the
user to continue.
3- Utility: Work that may take a bit and doesn’t need to finish right away. Analogous to progress bars and importing
data.
4-Background: This work isn’t visible to the user. Backups, syncs, indexing, etc.
A race condition occurs when two or more threads can access shared data and they try to change it at the same time.
Page 23 of 29
A Priority inversion is a critical condition in threading where a low priority thread blocks a high priority thread from
executing and make the assigned priorities meaningless for the thread.
60-What is deadlock?
A deadlock occurs when two or sometimes more tasks wait for the other to finish, and neither ever does.
61-What is concurrency?
Concurrency is the structural way to run multiple tasks at the same time.
62-What is NSOperation?
NSOperation is built on top of GCD. While using NSOperation developer can add dependency among various operations
and re-use, cancel and suspend operations. Of course the same thing can also be archived through GCD but it will add
extra overhead.
63-What is semaphore?
Semaphores gives us the ability to control access to a shared resource by multiple threads. A semaphore consist of a
threads queue and a counter value. Counter value is used by the semaphore to decide if a thread should get access to
a shared resource or not. The counter value changes when we call signal() or wait() functions.
Page 24 of 29
64-What is design pattern and explain brie y some commonly used Cocoa design patterns?
Design patterns are reusable solutions to common problems in software design. Design patterns can speed up the
development process by providing tested, proven development paradigms. Effective software design requires
considering issues that may not become visible until later in the implementation. Reusing design patterns helps to
prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar
with the patterns.
Commonly used Cocoa design patterns are –
Creational Design Pattern: Singleton, Factory
Structural Design Pattern: Decorator, Adapter, Facade
Behavioral Design Pattern: Observer, Memento.
65-What is Singleton Pattern and why should we avoid over using it?
The singleton pattern(Creational) guarantees that only one instance of a class is instantiated for the lifetime of the
application. It is very easy pattern to implement but it also has bad reputation to misuse. Singleton kills the idea of
“separation of concern” as it can be accessed from everywhere(They are generally used as a global instance). Singleton
violates the single responsibility principle .
66-What is Singleton Pattern and why should we avoid over using it?
Page 25 of 29
fl
Singletons are a problem from a testing perspective. They tend to make isolated unit-tests difficult to write because one
test can change the global value which is not valid for other tests.
The decorator design pattern(Structural) dynamically adds behaviours and responsibilities to an object without
modifying its code. Unlike inheritance, decorated objects are not limited by their parent classes. Putting in other terms,
a client has control over how and when to decorate the component. This pattern is simply achieved by using protocol in
Swift.
Factory method is a creational design pattern which solves the problem of creating objects without specifying their
concrete classes.
The facade design pattern provides a single interface to a complex subsystem. Instead of exposing the user to a set of
classes and APIs, you only expose one simple unified API. In Swift, you can achieve this pattern using extensions and
delegation.
Page 26 of 29
67-What are the basic differences between creational, structural and behavioural pattern?
The creational pattern provides way to create objects while hiding the creational logic which offers the flexibility for
creating objects based on different use cases.
The structural pattern helps us to manage classes and object together to create larger components.
The behavioural pattern helps us to provide better communication between objects and increase flexibility between
object.
The adapter design pattern allows two objects, with related functionalities, to work together, even when they have
incompatible interfaces. Adapter allows the objects to cooperate with other objects where they could not normally work
with due to different interfaces. It is a structural design pattern which is useful for composing classes and objects into a
larger system. Swift does not support multiple inheritance but Swift supports conformance to multiple protocols, you
can implement adapter pattern by using protocols.
Page 27 of 29
Observer pattern is a behavioural design pattern where the objects can notify other objects about the changes in their
state. The Observer pattern provides a way to subscribe and unsubscribe to and from these events for any object that
implements a subscriber interface.
We can implements the observer pattern in two ways in Swift – Notifications and Key-Value Observing.
Delegation is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to
an instance of another type. The delegating object typically keeps a reference to the other object (delegate) and sends
a message to it at the appropriate time.
74-What is POP?
Protocol-Oriented Programming is a new programming paradigm ushered in by Swift 2.0. In the Protocol-Oriented
approach, we start designing our system by defining protocols. We rely on new concepts: protocol extensions, protocol
inheritance .
In Swift, value types are preferred over classes. However, object-oriented concepts don’t work well with structs and
enums: a struct cannot inherit from another struct, neither can an enum inherit from another enum.
On the other hand, value types can inherit from protocols, even multiple protocols. Thus, with POP, value types have
become first-class citizens in Swift.
Page 28 of 29
75-How would you securely store private user data of ine on a device? What other security best practices
should be taken?
If the data is extremely sensitive then it should never be stored offline on the device because all devices are crackable.
The keychain is one option for storing data securely. However it's encryption is based on the pin code of the device.
User's are not forced to set a pin, so in some situations the data may not even be encrypted. In addition the users pin
code may be easily hacked.
76-Could you explain what is the difference between Delegate and KVO?
Both are ways to have relationships between objects. Delegation is a one to one relationship where one object
implements a delegate protocol and another uses it and sends messages to assuming that those methods are
implemented since the receiver promised to comply to the protocol. KVO is many-to-many relationship where one
object could broadcast a message and one or multiple other objects can listen to it and react.
Page 29 of 29
fl