Quick Note_ : If you want to be updated of this series , go through this __link _or check comment section of this Article .
“You will get answer for given question in first few line , and after that there will be some insights and key points about that question .”
In short its an ability of swift . You dont always need to write types of variables and constant you making in your code . For example :
// swift know its Int type
var age = 40 // Int
// You dont need to tell them always like below
var age : Int = 40
Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define.
100 + Que/Ans Series , I will post a continue series on iOS Interview questions from a basic concept to advance .
Suppose you want to swap to values of type Int , lets write a non-generic functions :
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
var num1 = 4
var num2 = 5
swapTwoInts(&num1 , &num2)
Now , suppose you want to swap two double values or two string values , you will need to write another function for that , because the above function is accepting only for Int type .
#interview-questions #ios #interview #programming #swift