Top IOS Interview Questions and Answers (April 2019) Part II
Q1. Why do we use application life cycle?
1. Manages interactions between the system and the app — To utilize system resources and execution time given by system based on application states.
2. Manages your app’s high-level behavior, Customizations can range from providing an icon for your app to making architectural-level decisions about how your app presents and uses information.
Q2. How to run application without any views in iOS?
Create a new project with single view application, then delete
all main and launch storyboard file, delete the entry of main
storyboard from info.plist file.
Add your code in
didfinishlaunchingwithoptions api and run the application.
Black
Screen will appear without any views and your code will be
executed.
Q3. Is it possible that an iOS application can have more than one window?
- It is not a problem at all to add another UIWindow. Just create one and makeKeyAndVisible. Done.
- Remove it by making another window visible, then release the one you don’t need anymore.
- The window that is “key” receives all the keyboard input.
- UIWindow covers everything, even modals, popovers, etc. Brilliant!
- UIWindow is always in portrait implicitly. It does not rotate.
- You’ll have to add a controller to the new window’s root controller and let that handle rotation.
- (Just like the main window) The window’s level determines how “high” it gets displayed. Set it to UIWindowLevelStatusBar to have it cover everything.
- Set its hidden property to NO. A 2nd UIWindow can be used to bring views on the screen that float on top of everything. Without creating a dummy controller just to embed that in a UIPopoverController.
- It can be especially useful for iPhone where there is no popover controller but where you might want to mimic something like it.
- And yes, it solved, of course, my problem: if the app resigns activation, add a cover window over whatever is currently shown to prevent iOS from taking a screenshot of your app’s current content.
Q4. What do you mean by coupling?
An indication of the strength of interconnections between
program units.
Highly coupled have program units dependent
on each other. Loosely coupled are made up of units that are
independent or almost independent.
COHESION Measure of how well module fits together.
Q5. How do you handle STORYBOARD MERGE CONFLICTS?
There are five general situations where merge conflicts are
often encountered in storyboards.
1.
Document Info:
The merge conflict will happen when one user edits and commits a
storyboard in an older version of Interface Builder, and another
user edits and commits the same storyboard in the newer version
of Interface Builder.
Xcode has updated the “document” tag and the “plugIn” tag in
the storyboard’s source.
The best solution is just to select the newer version to resolve
the conflict and merge.
To avoid this type of merge
conflict, it’s advisable for teams to coordinate Xcode updates.
2. Automatic Updates: Interface Builder will automatically update a storyboard seemingly every time a developer opens it; frequently the updates are caused by opening a storyboard on a different machine with a different monitor size or type.
The merge conflicts are pretty easily identifiable as small changes in the frame or layout information for views throughout the document. To avoid this type of conflict, don’t commit automatic changes reflexively. Developers should evaluate if they actually made a change to a storyboard, and only include it in the commit if they did.
3. Resources Section: This type of conflict occurs when two developers use images that haven’t been used before in the storyboard. The images can be used in buttons, image views, or any other view that can display an image. References to the images get added to the Resources section in the storyboard’s XML and can cause a merge conflict.
Fortunately a merge conflict in the Resources section is trivial to resolve — it’s very similar to a merge conflict when adding new files to an Xcode project file. Select one of the “both sides” options to merge the changes; the order does not matter.
4.
Multiple New Scenes: This conflict can occur when two developers each add a new
scene to a storyboard and the storyboard adds the new scenes to
the same place in the XML.
There are two approaches
to resolve the conflict, depending on how the diff ends up. If
the diff includes the entire scene on each side, then select one
of the “both side” options to merge the changes; the order does
not matter.
Second approach: use a text editor to move the
scene from one side below the scene from the other side, then
remove all the conflict tags. Be consistent when removing the
conflict tags for each scene — if the scene was on the right,
remove only the left side of the conflict for that scene.
5. Editing the Same Scene: This type of conflict occurs when two developers make edits to the same scene.
The best approach with “Editing the Same Scene” type conflicts is to avoid them in the first place. Be careful when assigning tasks to developers to segregate them across scenes; if multiple tasks affect the same scene in a storyboard, see if the tasks can either be combined or can be assigned to the same developer to coordinate the changes and avoid a conflict.
Reference: http://martiancraft.com/blog/2018/02/handling-storyboard-merge-conflicts/
Q6. What is Optional Chaining?
Optional chaining is a
process for querying and calling properties, methods, and
subscripts on an optional that might currently be
nil
. If the
optional contains a value, the property, method, or subscript
call succeeds; if the optional is
nil
, the
property, method, or subscript call returns
nil
. Multiple
queries can be chained together, and the entire chain fails
gracefully if any link in the chain is
nil
.
if let johnsStreet =
john.residence?.address?.street
{
print(“John’s street name is \(johnsStreet).”)
}
else {
print(“Unable to retrieve the address.”)
}
Reference: https://docs.swift.org/swift-book/LanguageGuide/OptionalChaining.html
Q7. What are available type of Core Data migration?
There are two types of migrations:
- lightweight migrations
- heavyweight migrations
Heavyweight migrations are pretty complex and you should try to avoid them whenever possible. Lightweight migrations are much easier because Core Data takes care of the heavy lifting for us.
For Core Data to be able to generate an inferred mapping model, changes must fit an obvious migration pattern, for example:
- Simple addition of a new attribute
- Removal of an attribute
- A non-optional attribute becoming optional
- An optional attribute becoming non-optional, and defining a default value
- Renaming an entity or property
To add support for lightweight migrations to the
CoreDataManager
class, we need to make a minor change. Do you remember that
addPersistentStore(ofType:configurationName:at:options:)
accepts a dictionary of options as its last parameter? To add
support for migrations, we pass in a dictionary of options with
two keys:
-
NSInferMappingModelAutomaticallyOption
: If the value of this key is set totrue
, Core Data attempts to infer the mapping model for the migration based on the data model versions of the data model. -
NSMigratePersistentStoresAutomaticallyOption
: By setting the value of this key totrue
, we tell Core Data to automatically perform a migration if it detects an incompatibility.
Use heavyweight (manual) migration in rare cases when changes to the data model exceed the capabilities of lightweight migration.
Q8. How will you create your own pod?
Running
pod lib create [pod name]
will set you up with a well thought out library structure
allowing you to easily include your files and get started
quickly.
For more: https://www.raywenderlich.com/5823-how-to-create-a-cocoapod-in-swift
Q9. What is HomeKit framework?
HomeKit: Allow users to communicate with and control connected accessories in their home using your app. With the HomeKit framework, you can provide a way to configure accessories and create actions to control them. Users can even group actions together and trigger them using Siri.
HomeKit requires additional configuration in your Xcode project. Your app must be provisioned and code signed to use HomeKit. To avoid code signing issues, enable HomeKit in the Xcode Capabilities pane.
HomeKit allows users to create a layout for one or more of their
homes. A home (HMHome
) represents a single dwelling that has a network of
accessories. A home’s data is owned by the user and can be
accessed from any of the user’s iOS devices. A user can also
share a home with a guest who has more limited access. One home
is designated as the primary home and is the default home for
Siri commands that don’t specify a home.
Each home may have multiple rooms and accessories added to each
room. A room (HMRoom
) represents an individual room in the home. It has a
meaningful name, such as “living room” or “kitchen," which
can be used in Siri commands. An accessory (HMAccessory
) is a physical home automation device, such as a garage door
opener. A service (HMService
) is an actual service provided by an accessory, such as
opening and closing a garage door or the light on the garage
door opener.
Q10. What is promises & PromiseKit on iOS?
Asynchronous programming can be a real pain and can easily result in messy code. Fortunately for you, there’s a better way using promises & PromiseKit on iOS.
When you create a PromiseKit
Promise
, you’ll
provide your own asynchronous code to be executed. Once your
asynchronous work completes, you’ll fulfill your
Promise
with a
value, which will cause the Promise’s
then
block to
execute. If you then return another promise from that block, it
will execute as well, fulfilled with its own value and so on. If
there is an error along the way, an optional catch block will
execute instead.
PromiseKit is a Swift implementation of promises. While it’s not the only one, it’s one of the most popular. In addition to providing block-based structures for constructing promises, PromiseKit also includes wrappers for many of the common iOS SDK classes and easy error handling.
Reference: https://www.raywenderlich.com/9208-getting-started-with-promisekit
Q11. What is Generics in swift?
Generics are used to avoid duplication and to provide abstraction.
Generic functions can
work with any type. Here’s a generic version of the
swapTwoInts(_:_:)
function from above, called
swapTwoValues(_:_:)
:
func swapTwoValues<T>(_ a: inout T, _ b: inout T) {
let temporaryA = a
a = b
b = temporaryA
}
Hope this article is useful for people who are looking out for change, Please ❤️ to recommend this post to others 😊. Let me know your feedback. :)