Swift Interview Questions – Set 01

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.

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.

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.

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.

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.

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.

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!)”)
}

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.

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.

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*/