Top IOS Interview Questions and Answers (Jan 2019) Part 4

This article includes questions from Viacom for IOS position from f2f round (3rd or 4th). Hope it will help you.

Top IOS Interview Questions and Answers (Jan 2019) Part 4

This article includes questions from Viacom for IOS position from f2f round (3rd or 4th). Hope it will help you.

Round 1:

1. When to use Userdefault, Keychain & CoreData?

Ans:

Userdefault
Avoid storing image data (conversion of UIImage to NSData) into UserDefaults, as UserDefaults are not meant to store large amount of data.
Storing large amount of data into UserDefaults could affect performance of your app significantly as the whole UserDefaults plist file is loaded into memory when your app launches. As mentioned in Apple Documentation :

UserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value.

Reference: https://developer.apple.com/documentation/foundation/userdefaults

Keychain
The keychain services API helps you solve this problem by giving your app a mechanism to store small bits of user data in an encrypted database called a keychain. When you securely remember the password for them, you free the user to choose a complicated one.

You should always use Keychain to store sensitive data like password, keys, certificates etc.

Data saved in Keychain can be accessed by multiple apps, provided that the data are created from the apps from the same developer. This is how SSO (Secure sign on, like you login in one app and then another app will auto login for you) in iOS app works.

Starting in iOS 8, you can store data in the keychain that can only be accessed after the user successfully authenticates on the device with Touch ID or a passcode. When it’s time for the user to authenticate, Touch ID will take priority if it is set up, otherwise the passcode screen is presented. Saving to the keychain will not require the user to authenticate, but retrieving the data will.

In case of large data stored in keychain, you are wasting the user’s LTE bandwidth, since every change will be sent over the internet to every device they own. Basically keychain is encrypted multiple times, because it needs to be decrypted in many different situations with different needs/security levels. That might explain part of why it’s so slow.

Note: iOS 10.3 or later auto deletes keychain items after application uninstall. This is an intentional change in iOS 10.3 to protect user privacy. Information that can identify a user should not be left on the device after the app that created it has been removed.

It has never been a part of the API contract that keychain items created by an app would survive when the app is removed. This has always been an implementation detail.

If a keychain item is shared with other apps, it won’t be deleted until those other apps have been deleted as well.

CoreData

From Dave DeLong ‘s blog post on Core Data :

Core Data is an “object graph and persistence framework”, which is basically like a fancy kind of object-relational mapping. That means it is a whole bunch of code to help you maintain a graph (ie, a “network” of related pieces of data with a defined organization) of objects and then persist them in some fashion.

Core Data is great for when you have a long list of data (eg: to-do list, list of bookmarks etc) to save / load. Especially if your data have some relationships (eg: Order with multiple items) , require custom query / filtering (eg: Getting items below certain price) or require sorting function (sort the retrieved data by price), Core Data can handles these for you out of the box.

2. What is Closures, Why to use closures? or

Difference between delegates and closures? Which one to prefer?

Ans: First of all, there is nothing that is impossible without using closures. You can always replace a closure by an object implementing a specific interface. It’s only a matter of brevity and reduced coupling.

Comparison

1. Length and Relationship

The closure way is shorter. When you work with the delegate way, you have to implement the required functions to conform to the protocol.When you use the closure way, even if the object calls the method, Class can simply ignore. Therefore, the closure way is often called, "decoupled". The relationship is rather weak and fragile.

Some would argue that you can make the delegate function as “optional”. But, if you see your code , it still looks complicated.

2. Memory Management

When it comes to memory management, you have to define the protocol as class and define the delegate property as weak to prevent retain cycle.

When you work with the closure way, you have to define self either withunowned or weak. If you are not using self, you don't have to worry about retain cycle.

3. Backward Communication

If you want to communicate back to the object, using the delegate way, you have to use the datasource pattern. If you want to communicate back to the object using the closure way, you also have to return something in closure.
Well, there isn’t much difference.

4. Dryness

If you want to implement many other delegate protocols, you probably have to include all protocol and implement methods, which looks hideous and you will have a hard time tracking and debugging. Good luck. Yes, we’ve tried to manage and distribute through extension but it will still be tough my friend.

But when it comes to the closure way, it wins.

Even other platforms are moving from delegate to closure way, React and React Native use the closure way.

Features

  1. Shorthand argument names. reversedNames = names.sorted(by: { $0 > $1 }
  2. If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead.
  3. One of the key advantages of closures in Swift is that memory management is something you, the developer, don’t have to worry about.
  4. The basic syntax of a closure isn’t difficult.
  5. Value capturing is great feature of swift closure.
  6. It is sometimes useful to write shorter versions of function-like constructs without a full declaration and name.
  7. Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios.
  8. Inferring parameter and return value types from context.
  9. Implicit returns from single-expression closures.

3. Difference between Array and NSArray? How to choose between value type & reference types?

Ans: In a nutshell, Swift changed array from reference type to value type because it has spirit of improving safety.

Arrays, dictionaries etc. in Objective-C are often mutable. That means when I pass an array to another method, and then that array is modified behind the back of the other method, surprising (to put it gently) behaviour will happen.

By making arrays, dictionaries etc. value types, this surprising behaviour is avoided. When you receive a Swift array, you know that nobody is going to modify it behind your back. Objects that can be modified behind your back are a major source for problems.

How to Choose?

So if you want to build a new type, how do you decide which kind to make? When you’re working with Cocoa, many APIs expect subclasses of NSObject, so you have to use a class. For the other cases, here are some guidelines:

Use a value type when:

  • Comparing instance data with == makes sense
  • You want copies to have independent state
  • The data will be used in code across multiple threads

Use a reference type (e.g. use a class) when:

  • Comparing instance identity with === makes sense
  • You want to create shared, mutable state

In Swift, Array, String, and Dictionary are all value types. They behave much like a simple int value in C, acting as a unique instance of that data. You don’t need to do anything special — such as making an explicit copy — to prevent other code from modifying that data behind your back. Importantly, you can safely pass copies of values across threads without synchronization. In the spirit of improving safety, this model will help you write more predictable code in Swift.

4. Difference between UDID and VendorID?

Ans:
UUID (Universally Unique Identifier): A sequence of 128 bits that can guarantee uniqueness across space and time, defined by RFC 4122.

UDID is deprecated in iOS 5.

What are the alternatives?

identifierForVendor and advertisingIdentifier.

What is identifierForVendor?

identifierForVendor is an same UUID for all the apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.

What is a vendor?

A vendor is defined by the first two parts of the reverse DNS formatted CFBundleIdentifier. For example, com.2359media.app1 and com.2359media.app2 would have the same identifierForVendor. But my.hungrygowhere.iphoneapp and com.2359media.hgwm would get a different identifierForVendor.

How do I get identifierForVendor?

NSString *vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

Is identifierForVendor persisted?

NO. identifierForVendor remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them.

What is advertisingIdentifier?

advertisingIdentifier is an UUID for each device, used only for serving advertisement. Unlike the identifierForVendor, the same value is returned to all vendors.

How do I get advertisingIdentifier?

NSString *adID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

Is advertisingIdentifier persisted?

No. advertisingIdentifier remains the same value until:

  • The user does a full system reset (Settings.app -> General -> Reset -> Reset All Content and Settings)
  • The User explicitly resets it (Settings.app -> Privacy -> Advertising -> Reset Advertising Identifier)

References: https://developer.apple.com/documentation/uikit/uidevice/1620059-identifierforvendor
https://gist.github.com/hujunfeng/6265995

5. What is the app store submission process?

Ans:

Prerequisites

  1. Project that is ready for submission and passes Apple’s App Store Guidelines
  2. Valid Apple developer program account (not the Apple Developer Enterprise Program)
  3. Computer that runs Mac OS X
  4. The following programs installed:

Xcode

Keychain Access

Step Overview

Below you will find a list of guides, each describing how to do one of the steps required for submitting an app.

  1. Assemble App Store Information
  2. Create a Bundle Identifier
  3. Create a Certificate Signing Request
  4. Create an App Store Production Certificate
  5. Create a Production Provisioning Profile
  6. Create an App Store Listing
  7. Create a Release Build
  8. Fill in the Version Information
  9. Submit Version for Review
  10. Release

References: https://clearbridgemobile.com/how-to-submit-an-app-to-the-app-store/

If you have any comment, question, or recommendation, feel free to post them in the comment section below! You can also follow me on Medium for new articles and connect with me on LinkedIn.