Google News
logo
Swift Interview Questions
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*/
There 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.

Declare an empty array
Syntax :
let emptyIntArr:[Int] = []  
print(emptyIntArr)  

Output :

[]

 

Create an array directly : Swift is a type inference language so, we can also create an array directly without specifying the data type but we have to initialise with some values so that compiler can finds out its type.
 
Syntax :
let someIntArr = [1, 2, 3, 4, 5]  
print(someIntArr)  
Output :
[1, 2, 3, 4, 5]
 
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.

Declaring an empty dictionary : To create an empty dictionary, we specify the key:value Data type inside square brackets [].
 
Syntax :
let emptyDictionary:[Int:String] = [:]  
print(emptyDictionary) 
 
Output :
[:]
Declaring a dictionary with some values
let valDictionary = ["a":10, "b":20, "c":30, "d":40, "e":50, "f":60, "g":70, "h":80, "i":90]  
print(valDictionary)  
Output :
["c": 30, "d": 40, "g": 70, "b": 20, "a": 10, "f": 60, "h": 80, "i": 90, "e": 50]
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.
Floating numbers are numbers with a fractional component, like 3.25169 and -238.21.  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 must be very large

* Float :
It represents a 32 bit floating point number, it is used when floating point values does not need 64 bit precision
A de-initializer is declared immediately before a class instance is de-allocated.  You write de-initializer with the deinit keyword.  De-initializer is written without any parenthesis, and it does not take any parameters. It is written as
deinit  {

// perform the deinitialization

}
Lazy stored properties are used for a property whose initial values is not calculated until the first time it is used.  You can declare a lazy stored property 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.
It allows you to provide a new name to the existing data type in the programming. After declaring the alias name to the existing type, we can use it throughout the programming. It will provide more readability to the code and easy understandability.
 
typealias AudioS1 = UInt16
Adapter : It converts the interface of a class into other interfaces as required by the client. It covers itself around the objects to reveal a standard interface to connect with that object.
 
Memento : It is used in iOS as a part of the state restoration. This externalized state of the code can be store without breaching encapsulation. Memento is especially used by Apple for archiving.
@synthesize : It is used to generate a getter and setter method for the property.
@property (nonatomic, retain) NSButton *someButton1;

…

@synthesize someButton1;
 
 
@dynamic : It will inform the compiler that getter and setter are implemented at some other place.
@property (nonatomic, retain) IBOutlet NSButton *someButton1;

…

@dynamic someButton1;
let,’ and ‘var’ are used to declare the functions in JavaScript. The main difference in both of them is that ‘let’ is block-scoped, whereas ‘var’ is the functional scope. As variable is represented by ‘var,’ and it is defined in the whole program in relation to ‘let.’
 
Example for defining "var" :
 
Syntax :
console.log(y);

var y=7;

console.log(y);

 

Output : undefined 7


Example for defining "let" :
 
Syntax :
console.log(y);

let x=7; console.l

og(y);

 

Output : Error