Page 226

32. Swift – Generics

Swift

Swift language provides 'Generic' features to write flexible and reusable functions and types. Generics are used to avoid duplication and to provide abstraction. Swift standard libraries are built with generics code. Swifts 'Arrays' and 'Dictionary' types belong to generic collections. With the help of arrays and dictionaries the arrays are defined to hold 'Int' values and 'String' values or any other types. func exchange(inout a: Int, inout b: Int) { let temp = a a = b b = temp }

var numb1 = 100 var numb2 = 200

println("Before Swapping values are: \(numb1) and \(numb2)") exchange(&numb1, &numb2) println("After Swapping values are: \(numb1) and \(numb2)") When we run the above program using playground, we get the following result: Before Swapping values are: 100 and 200 After Swapping values are: 200 and 100

Generic Functions: Type Parameters Generic functions can be used to access any data type like 'Int' or 'String'. func exchange<T>(inout a: T, inout b: T) { let temp = a a = b b = temp }

var numb1 = 100 var numb2 = 200 214

​Fast is really a strong and intuitive coding language for macOS, iOS, watchOS and tvOS....  

Quick is a effective and instinctive programming language for macOS, iOS, watchOS and tvOS....

Read more
Read more
Similar to
Popular now
Just for you