Interview GURU

Swift interview questions

There are most popular Swift interview questions asked in many companies.

1. What is Swift? How is it different from Objective-C?

Swift and Objective-C both are used in iOS development but both are significantly different in terms of efficiency and usage.
  • Swift is an open-source programming language developed by Apple platform and expanded to build on Linus while Objective-C is not an open-source programming language and is limited to Apple.
  • Swift syntax is easy, clear and brief. It makes API's easy to read and maintain while Objective-C is based on C language which is comparatively hard to use.
  • Swift is more rational and precise that's why it has less number of code and easy to learn while Objective-C code is lengthy as double to Swift code.
  • Swift can be compiled as a dynamic framework while Objective-C cannot be compiled into static libraries and dynamic framework.

2. What is the meaning of question mark "?" in Swift?

In Swift, question mark "?" is used in property declaration. It tells the compiler that this property is optional. The property may hold a value or not. It avoids runtime errors when accessing that property by using ?. This is useful in optional chaining and a variant of this example is in conditional clauses.

For example:
var optionalName : String? = "John"  
if optionalName != nil {  
    print("Your name is \(optionalName!)")  

More details...

3. How can you make a property optional in Swift?

We have to declare a question mark ??' in the code to make a property optional. If a property doesn't have a value, then the symbol ? helps to avoid the runtime error.

4. What are the Half Open Range operators in Swift?

Swift supports several types of operators. One of them is Half Open Range operator. The half open range operator specifies a range between two values a and b (a<b) where b is not included. It is known as half open range operator because it contains its first value only not the final value.

More details...

5. What are functions in Swift?

Functions are the set of codes used to perform specific task. In Swift programming language, functions are used to pass local as well global parameter values inside the function's call.

In Swift4, functions can be categorized in two types:
  • User Defined Functions
  • Built-in Functions (Library Functions)

6. What is Nested Function in Swift?

A function inside a function is called a nested function.

func function1() {    
    //statements of outer function   
    func function2() {    
        //statements of inner function    

More details...

7. Explain Enum in Swift.

Enum is also known as Swift Enumeration. Enum is a data type which contains a set of the related values. It is declared in a class and its values are accessed through the instance members of that class.

enum enum_name    
// values are described here    

More details...

8. What are Regular Expression and Responder Chain in Swift?

Regular Expression: Regular expressions are the special string patterns that specify how to perform a search through a string.

Responder Chain: Responder Chain is a hierarchy of objects that obtain the opportunity to respond to the events.

9. Explain Dictionary in Swift.

Swift Dictionary is used to store the key-value pairs and access the value by using the key. It is just like hash tables in other programming languages.

More details...

10. How would you define variables and constants in Swift programming language?

You have to declare variables and constants before using it. Constants are declared by using let keyword and variables by var keyword.

var interviewGuru = "This is Interview Guru"  
let fees = 10000

More details...

11. Explain the different features of Swift programming language?

Features of Swift programming language:
  • Swift is very easy to learn and precise to use. It provides a safe programming approach.
  • In Swift programming language, variables and constants must be initialized before use.
  • Automatic memory management.
  • Swift uses "switch" function instead of "if" statement for conditional programming.
  • Swift follows Objective-C like syntax.
  • It checks Arrays and integers for overflow.

12. What type of literals does Swift language have?

A Swift literal is a direct value of a variable or a constant. It may be a number, character or string. Literals are used to initialize or assign value to variables or constants.

Different types of literals are:
  • Binary Literals
  • Octal Literals
  • Hexadecimal Literals
  • Decimal Literals

13. What is floating point number in Swift? What are the different floating point numbers in Swift?

Numbers with decimal values or fractional components are called floating numbers. For example: 1.34 is a floating point number. Floating point types can represent a wider range of values than integer types. There are two signed floating point number:

Double: It represents a 64 bit floating point number. It is used when floating point values are very large.

Float: It represents a 32 bit floating point number. It is used when floating point values does not need 64 bit precision.

14. How can you write a comment in Swift?

In Swift programming language, single-line comments are started with double slashes (//).

For example:
// This is a single line comment.  

Multi-line comment: Multiline comments are started with a forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward-slash (*/).

For example:
/* this is multi 
Line comment*/  

More details...

15. What are the different control transfer statements used in Swift?

Swift language consists of following Control transfer statements:
  • Continue
  • Break
  • Fallthrough
  • Return

16. What do you mean by Optional Chaining in Swift?

In Swift programming language, Optional Chaining is a process of querying and calling properties. You can chain multiple queries together, but if any link in the chain is nil then, the entire chain fails.

17. What is lazy stored procedure in Swift and when is it used?

Lazy stored properties are used for a property whose initial values are not calculated until the first time it is used. A lazy stored property can be declared by writing the lazy modifier before its declaration. Lazy properties are useful when the initial value for a property is reliant on outside factors whose values are unknown.

18. What is the usage of switch statement in Swift language?

  • Switch statement are used as a substitute for the long if-else-if statements.
  • Switch statement supports any type of data, synchronizes them and also checks for equality.
  • Break is not required in switch statement because there is no fall through in switch statement.
  • Switch statement must have covered all possible values for your variable.

19. What is the use of break statement in Swift language?

The break statement is used within a loop where you have to immediately terminate a statement. It is also used to terminate a case in switch statement.

20. What is the use of continue statement in Swift loop?

The continue statement is used in Swift loop to change execution from its normal sequence. It stops currently executing statement and starts again at the beginning of the next iteration through the loop.

21. What are the different collection types available in Swift?

Here are two varieties of collection types in Swift:

Array: In Swift, you can create an array of single type or an array of multiple type.

Dictionary: In Swift, dictionary is similar to hash table in other programing language. You can store a key-value pair in a dictionary and access the value by using the key.

22. What is Inheritance in Swift?

Inheritance is a process in which a class can inherit properties, methods and other characteristics from another class. Inheritance is supported in Swift programming language. There are two types of classes in Inheritance in Swift:

Sub class: The class which inherits the properties from other class is called child class or sub class.

Super class: The main class from where the subclass inherits the properties is known as parent class or super class.