Posts

Showing posts from June, 2020

what is Swift - Closures?

Syntax Following is a generic syntax to define closure which accepts parameters and returns a data type − { (parameters) −> return type in statements } Following is a simple example − let studname = { print ( "Welcome to Swift Closures" ) } studname () When we run the above program using playground, we get the following result − Welcome to Swift Closures The following closure accepts two parameters and returns a Bool value − { (Int, Int) −> Bool in Statement1 Statement 2 --- Statement n } Following is a simple example − let divide = { ( val1 : Int , val2 : Int ) -> Int in return val1 / val2 } let result = divide ( 200 , 20 ) print ( result ) When we run the above program using playground, we get the following result − 10 Expressions in Closures Nested functions provide a convenient way of naming and defining blocks of code. Instead of representing the whole function declaration and name constructs are used to ...

Distinguish between @synthesize and @dynamic in Objective –C?

@synthesize – It generates the getter and setter methods for the property. @dynamic – It notifies the compiler that the getter and setter are implemented at some other place.

What is benifit of using higher order functions?

Higher-order functions are functions that take one or more functions as parameters or return a function as their result. In Swift, higher-order functions are an essential part of the functional programming paradigm. They provide several benefits: Conciseness and Readability: Higher-order functions allow developers to write more concise and expressive code. With functions like map , filter , reduce , and others, complex operations on collections can be expressed in a more readable and declarative manner, reducing the need for explicit loops and boilerplate code. Example using map : swift Copy code let numbers = [ 1 , 2 , 3 , 4 , 5 ] let squaredNumbers = numbers.map { $0 * $0 } print (squaredNumbers) // [1, 4, 9, 16, 25] Code Reusability: Higher-order functions promote code reusability by encapsulating common operations. Once a higher-order function is defined, it can be reused with different functions or applied to different types, making the codebase more modular. Example usi...