Swift Interview Questions

IOS swift is the latest programming language released by APPLE and is meant to be replacing the Objective-C and trying to corner the industry with this prominent programming language.

So, many people have started transitioning to Swift programming language from Objective-C or just trying to learn the swift with a Java background in order to cope up with the market requirement.

For those who are preparing for a junior developer interview, here are some questions that can be helpful for you to crack the interview and to carry on your future career in the programming industry.

Following are the list of some basic Swift interview questions and answers

#1 How can you make a property optional in Swift?

Declaring a question mark “?” in the code can make a property optional.
If a property doesn’t hold a value, then this symbol “?” helps in avoiding the runtime errors.

#2 What are the important data types found in Objective-C?

There are four data types that the developers mostly use in Objective – C.
  • BOOL – It represents a Boolean value that is true or false.
  • NSInteger – It represents an Integer.
  • NSString – It represents a string.
  • Cgfloat – It represents a floating-point value.

#3 How to call Objective-C code from Swift

Follow the following steps:

Step 1: Add Objective-C Implementation --.m: First of all, add a .m file to the class and then name it CustomObject.m.

Step 2: Add Bridging Header: At the time of adding your .m file, you receive a prompt with three options of YES, NO and cancel. You need to select YES

Step 3: Add Objective-C Header --.h: After that Add one more .h file and name it CustomObject.h.

Step 4: Build your Objective-C Class

In CustomObject.h

#import <Foundation/Foundation.h>
@interface CustomObject : NSObject
@property (strong, nonatomic) id someProperty;
- (void) someMethod;
@end
In CustomObject.m
#import "CustomObject.h"
@implementation CustomObject 
- (void) someMethod {
    NSLog(@"SomeMethod Ran");
}
@end

Step 5: The next step is to add Class to Bridging-Header

In YourProject-Bridging-Header.h

#import "CustomObject.h"

Step 6: At last, use your Object

In SomeSwiftFile.swift:

var instanceOfCustomObject: CustomObject = CustomObject()

instanceOfCustomObject.someProperty = "Hello World"

println(instanceOfCustomObject.someProperty)

instanceOfCustomObject.someMethod()

In the bridging header, there is no need to import explicitly.

#4 What are initializer in Swift?

#5 What is tuple? How to create a tuple in Swift ?

A tuple is used to arrange multiple values in a single compound Value. In tuple, it is not compulsory for value to be of the same type. It can be of any type.

For an instance, ("interview", 123) is a tuple having two values: one is string Type, and another one is an integer type.

To create a tuple: To create it, you should have tuple literals which are a list of values listed in tuple separated by the comma. For example, the point is a tuple created by tuple literals written in between a pair of parentheses. To change a tuple literal, use dot notation with tuple’s name and assign the desired value. Check example:

var point = (0, 0)
point.0 = 10
point.1 = 15
point // (10, 15)

#6 What are the control transfer statements in swift?

The control transfer statements in swift are:
  • Continue
  • Break
  • Fallthrough
  • Return

#7 Explain the difference between let and var in Swift Programming?

Both the terms 'let' and 'var' are used for declaring the functions in JavaScript but the main difference between these two terms is that the 'let' is block scoped while a 'var' is the functional scope. It is not wrong to say that a variable represented with var is defined throughout the program in relation to let

Example of var:

Input: 
console.log(x); 
var x=5; 
console.log(x); 
Output: undefined 5

Example of let:

Input: 
console.log(x); 
let x=5; console.l
og(x); 
Output: Error

#8 What is forced unwrapping? Why is it potentially unsafe?

#9 What do you mean by the term “defer”?

The term “defer” is a keyword that provides a block of code that executes when the execution is leaving the current scope.

#10 How can you prioritize the usability of the demand Process?

  • Firstly, think like a user while designing the UX.
  • Consider all the situations that prove to be useful while promoting an App.
  • Continue to work on the utility of the App, even after the launch.

#11 Explain Deinitializer in Swift programming Language?

In Swift, a deinitializer is used to free up the resources by deallocates your instance class when they remain no longer useful for you. a dinit keyword is used to represent it. It is only present on class types.

#12 In Swift What is use of backticks ` while declaring a variable?

Backticks (`) are used as a name and to surround the keyword in the case if you want to give the same name as a reserved Swift keyword to a constant or a variable. But it is strongly advised to ignore the keyword unless you have no choice available.

Also Prepare: Frequently Asked Cocoa Touch Interview Questions

#13 What are the differences between a struct and a class?

#14 Explain some common execution states in iOS?

The states of the common execution can be as follows:
  • Not running – This state means that there is no code that is being executed and the application is completely switched off.
  • Inactive – This state means that the application is running in the background and is not receiving any events.
  • Active – This state means that the applications are running in the background and is receiving the events.
  • Background – This state means that the application is executing the code in the background.
  • Suspended – This state means that the application is in the background and is not executing.

#15 What are generics? How to make a method or variable generics in Swift?

Generic is efficient to write sensitive and reusable functions and types. It is provided in Swift 4. Most of the people use this feature to evade the risk of duplication. 'Arrays' and 'Dictionary' are also referred to generic collection. The libraries of Swift 4 are comprised of generic code.

You can add generics to a method (function) but can’t add to a variable. Understand it with an example:

func Vector3D(x: [T], y: [T], z: [T])
 {    self.xCord = x
    self.yCord = y
    self.zCord = z}

In this example, the generic created here is in a function. You can change the T with anything you want. This is how you can add generic in function.

Ask a Question