Search Your Question

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