Search Your Question

Difference between array, set and dictionary

Ans : 

Array :
Arrays are effectively ordered lists and are used to store lists of information in cases where order is important.
Cost of adding item in array is greater than set and dictionary if item is not appending first.

For example, posts in a social network app being displayed in a tableView may be stored in an array.

Set : 
Sets are different in the sense that order does not matter and these will be used in cases where order does not matter. Sets are especially useful when you need to ensure that an item only appears once in the set.

Dictionary : 
Dictionaries are used to store key, value pairs and are used when you want to easily find a value using a key, just like in a dictionary.


For example, you could store a list of items and links to more information about these items in a dictionary.


What is NSPredicate?

Ans : Using NSPredicate, we can filter records before fetching from Coredata or collections. It work in place of where clause in sql.

NSPredicate on Core Data :

When we want data from coredata of only who's name is Manan Shah. So using predicate we can filter as following :

var strName = "Manan Shah"
request.predicate = NSPredicate(format: "name = %@", strName)

NSPredicate on Collections : 

let manan = Person(firstName: "Manan", lastName: "Shah", age: 28)
let sagar = Person(firstName: "Sagar", lastName: "Shah", age: 26)

let people = [manan, sagar] as NSArray

let thirtiesPredicate = NSPredicate(format: "age >= 27")

people.filtered(using: thirtiesPredicate)
Result : manan object will be return.

What is safe area?

Ans: The Safe Area Layout Guide helps avoid underlapping System UI elements when positioning content and controls.

The Safe Area is the area in between System UI elements which are Status Bar, Navigation Bar and Tool Bar or Tab Bar. So when you add a Status bar to your app, the Safe Area shrink. When you add a Navigation Bar to your app, the Safe Area shrinks again.

On the iPhone X, the Safe Area provides additional inset from the top and bottom screen edges in portrait even when no bar is shown. In landscape, the Safe Area is inset from the sides of the screens and the home indicator.

The layout guide (Safe area) representing the portion of your view that is unobscured by bars and other content. In iOS 11+, Apple is deprecating the top and bottom layout guides and replacing them with a single safe area layout guide.

In iPhone X portrait, safe area insets from top(notch) and bottom(home) side, and
in landscape, left(notch/ home bar) and right side(home bar/notch) and also bottom side.

We can enable using of safe area layout guide property of view controller from class inspector.

Difference between SOAP and REST

Ans : 

1. Requests sent via REST tend to be much lighter than SOAP. Because of this, applications don't require much bandwidth to use REST web services over SOAP.

2. REST is good when working with web services open to the public, but if security is required, then the SOAP API is better to use. 

3. SOAP permits XML data format only while REST permits Plain text, HTML, XML, JSON etc.

i.e 

Suppose we want to retrieve today's weather report from a server which is providing weather information, your RESTful URL will look something like http://weatherdata.org/data/weather/uk/london (Not real link just example url), which is very similar to HTTP request like http://weatherdata.org/data/weather?q=uk,London.

On the other hand, in order to get the same data using SOAP, you need to create an XML message with header and body and send it http://www.webservicex.net/globalweather.asmx?op=GetWeather

In short, RESTfull web services are much simpler, flexible and expressive than SOAP web services in Java.

How many ways we can present view controller?

Ans : 

There are only 2 options to display one viewcontroller2 from viewcontroller1. It is depend on what we want to achieve.

1.Modal Presentation : Use this only viewcontroller2 should take focus away from viewcontroller1 entirely until it is dismissed.

[self presentViewController:viewController2 animated:YES completion:nil];

ViewController2's presenting viewcontroller is ViewController1.
ViewController1's presented viewcontroller is ViewController2.

UIAlertViewController is working on this way.

2. ViewController Containment : Use this if you want to display viewcontroller1's some view in which viewcontroller2's shows. So if there are 4 views, then in 4 views of viewcontroller1, we can show 4 different viewcontroller.

[self addChildViewController:viewController2];
[self.view addSubview:viewController2.view];
[viewController2 didMoveToParentViewController:self];

So here viewcontroller2's parent viewcontroller is viewcontroller1.
But viewcontroller1's child viewcontroller array contrains viewcontroller2.

UINavigationViewController is working on this way.

So interviewer ask questions about presented viewcontroller, parentviecontroller.

But in 1st way, parent viewcontroller is nil.
in 2nd way, presented viewcontroller, presenting viewcontroller is nil.

What is retain cycle or strong cycle?

Ans : 

According to ARC, variable should be one of three Strong, Weak or unowned. By default, variable has strong reference.

Let's take example :

class Employee {
var name  : String
var emid  : String
var title : String
init(name: String,emid: String, title: String) {
           self.name = name
   self.emid = emid
   self.title = title
        }

        deinit {
print("Employee : \(name) removed")
}
}

var manan : Employee? = Employee(name: "Manan", emid: "1234", title: "Sr iOS Developer")
manan = nil

Here deinit will be called and output will be "Employee Manan removed"

Now we add reference :

var sagar = manan
manan = nil

Here deinit will not be called due to adding reference. Here sagar has strong reference to variable which owned by manan before.

Now,

var sagar = manan
manan = nil
sagar = nil

Here deinit will be called and again output will be "Employee Manan removed"


Now let's discuss about Strong cycle :

Strong cycle occurs when two object has strong reference to each other. i.e objectA has strong reference to objectB and objectB has strong reference to objectA.


example :

class Employee {
var name  : String
var emid  : String
var title : String
var macBook : MacBook?
init(name: String,emid: String, title: String) {
           self.name = name
   self.emid = emid
   self.title = title
        }

        deinit {
print("Employee : \(name) removed")
}
}

class MacBook {
var serialNumber: String
var assignee : Employee?
        init(serialNumber: String) {
              self.serialNumber = serialNumber
        }

deinit {
print("Macbook : \(serialNumber) removed")
}
}

var manan : Employee? = Employee(name: "Manan", emid: "1234", title: "Sr iOS Developer")
var mac : MacBook? = MacBook(serialNumber: "ABC123")
manan?macBook= mac
manan = nil
mac = nil

Here, output :

Employee Manan removed
Macbook ABC123 removed

Why? Because here there is no strong reference from both side. So retain cycle is not yet created.

Now,

var manan : Employee? = Employee(name: "Manan", emid: "1234", title: "Sr iOS Developer")
var mac : MacBook? = MacBook(serialNumber: "ABC123")
manan?macBook= mac
mac?.assignee = manan
manan = nil
mac = nil

Output will be :

Nothing will be removed. Not output comes. Because of retain cycle. So there are strong relationship between both objects like manan has mac and mac is assigned to manan.

So, if we want to solve this problem we have to use weak or unowned reference for one side.

So,

weak var assignee : Employee?

Now output will be :

Employee Manan removed
Macbook ABC123 removed

So Now unowned : It is same as weak as it does not hold strong relationship with object. So when to use unowned : An unowned reference is used when the other instance has the same lifetime or a longer lifetime.

Difference between weak and unowned :


  • weak reference is used where there is possibility for that reference to become nil at some point during its lifetime. 
  • An unowned reference is used where there is no possibility for that reference becoming nil at any point until the self-object exist.
Example :

Every employee may or may not hold ICard but every ICard must be assigned to some one.
So Employee class has ICard variable as optional, but in ICard class, employee variable with optional and weak is not possible. If we want to use weak, we should make employee variable in ICard class optional. But we don't want that.

So, solution is that : 

unowned var employee : Employee

Now we are cleared about strong, weak and unowned and also our question : Retain Cycle or Strong Cycle.

Credit : Vinod Shwami

Application States

Ans : 

Before iOS 4.0 there were 3 states. Not running, inactive and active.

  1. Non-running - The app is not running.
  2. Inactive - The app is running in the foreground, but not receiving events. An iOS app can be placed into an inactive state, for example, when a call or SMS message is received.
  3. Active - The app is running in the foreground, and receiving events.
  4. Background - The app is running in the background, and executing code.
  5. Suspended - The app is in the background, but no code is being executed.
For maintaining above states, there are following app delegate methods:


application:willFinishLaunchingWithOptions (First method)
Method called when the launch process is initiated. This is the first opportunity to execute any code within the app.

application:didFinishLaunchingWithOptions
Method called when the launch process is nearly complete. Since this method is called is before any of the app's windows are displayed, it is the last opportunity to prepare the interface and make any final adjustments.

applicationDidBecomeActive
Once the application has become active, the application delegate will receive a callback notification message via the method applicationDidBecomeActive.

This method is also called each time the app returns to an active state from a previous switch to inactive from a resulting phone call or SMS.

applicationWillResignActive
 Each time a temporary event, such as a phone call, happens this method gets called. It is also important to note that "quitting" an iOS app does not terminate the processes, but rather moves the app to the background.

applicationDidEnterBackground
This method is called when an iOS app is running, but no longer in the foreground. In other words, the user interface is not currently being displayed. According to Apple's UIApplicationDelegate Protocol Reference, the app has approximately five seconds to perform tasks and return. If the method does not return within five seconds, the application is terminated.

applicationWillEnterForeground
This method is called as an app is preparing to move from the background to the foreground. The app, however, is not moved into an active state without the applicationDidBecomeActive method being called. This method gives a developer the opportunity to re-establish the settings of the previous running state before the app becomes active.

applicationWillTerminate
This method notifies your application delegate when a termination event has been triggered. Hitting the home button no longer quits the application. Force quitting the iOS app, or shutting down the device triggers the applicationWillTerminate method. This is the opportunity to save the application configuration, settings, and user preferences.



What is localization?

Ans : 

Localization is simply the process of translating your app into multiple languages. However, before you can localize your app, you first need to internationalize it. Internationalization is the process of making your app able to adapt to different languages, regions, and culture. Simple enough, I guess.

So after opening app, we can detect user's locale and change app's language,time accordingly.


Difference Between Inheritance And Extensions?

Ans : 

When to use extension : 
Are you adding general-purpose functionalities that should be available to every UITextField? If so, make an extension. All UITextField instances can call the new methods.

When to use inheritance : 
Are you adding functionality that should be restricted to special instances of UITextField that you would identify precisely? If so, make a subclass. Only the instances of the subclass can use the new methods.

Another diff :

In extension, there is no any specific name of extension in swift, but while subclassing there is another name of subclass.

In extension, we can not add variables (fields), in subclass it is possible to declare variables.


How To Access AppDelegate Methods In Other Class?

Ans : 

let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
appDelegate?.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)

You can make your custom method any called from viewcontroller like :

let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.anyAppDelegateInstaceMethod()

But above is only possible in iOS 10.0 or newer version.