Search Your Question

What is Enumeration in Swift?

Ans : 

What is enum?
An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.
Enumerations offer an easy way to work with sets of related constants. An enumeration, or Enum, is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.

When to use an enum?
Whenever a procedure accepts a limited set of variables, consider using an enumeration. Enumerations make for clearer and more readable code, particularly when meaningful names are used.

The benefits of using enumerations include:

  • Reduces errors caused by transposing or mistyping numbers. 
  • Makes it easy to change values in the future. 
  • Makes code easier to read, which means it is less likely that errors will creep into it. 
  • Ensures forward compatibility. With enumerations, your code is less likely to fail if in the future someone changes the values corresponding to the member names.



What are the app id and bundle identifier?

Ans : 
Bundle ID : 
A bundle ID or bundle identifier uniquely identifies an application in Apple's ecosystem. This means that no two applications can have the same bundle identifier. To avoid conflicts, Apple encourages developers to use reverse domain name notation for choosing an application's bundle identifier.
i.e com.iosiqa.newapp or com.iosiqa.app.new

App ID : App ID consists of Team ID + Bundle ID. Team ID is provided by apple to one team in developer account.
i.e ABC12345.com.iosiqa.newapp

What is use of APP ID? 
Whenever you want to enable a capability or application service for your application, you enable that capability for the App ID your application is linked to. This used to be tedious, requiring a visit to Apple's developer website. Xcode has evolved quite a bit over the years and it takes care of the details most of the time.

How To declare custom tableview cell? What Is The Use Of dequeuereusablecellwithidentifier ?

Ans : 

Let's create custom cell :

1. Take viewcontroller or tableviewcontroller
2. In Tableview, put one uitableviewcell and change style to custom.
3. Give any string (i.e "newcell") to reuseidentifier of cell in attribute inspector.
4. Put labels, textfields, views in cell according to your need.
5. Add new file named as newCell.swift that inherit from UITableViewCell.
6. Select your cell and give class name newCell.
7. Now you can make outlet of that cell in newCell.swift.

Here magic will be created using dequeuereusablecellwithidentifier : 

The best part of using dequeueReusableCellWithIdentifier is that using this you can re-use your cells.

Imagine if your table has 1000 entries. Now, if for each entry a table cell would be created then for 1000 entries, 1000 tableview cells and memory allocation for 1000 tableview cells.

App will be slowed down or would crash if the entries goes beyond 1000.

When we use, dequeueReusableCellWithIdentifier, the tableView just creates exactly the number of cells based on your table height and cell height. Suppose, if it shows 4 cells in the tabelView and rest you can see by scrolling, then memory for only 4 cells would be allocated at any given point of time.

Now, when you will scroll the tableView, it will re-use the same cell but will change the cell content (data) based on your data source.

Hope this clears your doubt.

Code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:"newcell"];
    cell.textLabel.text = @"Test";
    return cell;

}

Difference between App Bundle and File Directory

Ans : Although bundles and packages are sometimes referred to interchangeably, they actually represent very distinct concepts:

1.
A package is any directory that the Finder presents to the user as if it were a single file.
A bundle is a directory with a standardized hierarchical structure that holds executable code and the resources used by that code.

2.
The Finder considers a directory to be a package if any of the following conditions are true:
.The directory has a known filename extension: .app, .bundle, .framework, .plugin, .kext, and so on.
.The directory has an extension that some other application claims represents a package type; see Document Packages.

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