Showing posts with label Protocols. Show all posts
Showing posts with label Protocols. Show all posts

Monday, 1 October 2018

Extensions

Extensions means add additional functionality to existing types.
Extensions add new functionality to existing class, struct, enum or protocols.
Extensions are similar to categories in Objective-C.

Extension syntax
Declare Extension with "extension" keyword

extension Type {
    ///Additional functionality add hear

}

An Extension can extends one or more protocols.

extension Type : Protocol{
    ///protocols method hear
}

Example

extension String{
    ///Trim string value
    func toTrim() -> String{
        return self.trimmingCharacters(in: .whitespacesAndNewlines)
    }
}

///Simply use like below
let strName = " use of string extension "
print(strName)
print(strName.toTrim())

Output : 

 use of string extension 
use of string extension

Another example

extension UIView {
    func defaultRadius() {
        self.layer.cornerRadius = self.frame.size.width / 2
    }
}

let parentView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 50, height: 50))
parentView.backgroundColor = UIColor.red
parentView.defaultRadius()

Output: 

UIView Extension


Computed Properties

Extension can add computed instance property and computed type property to existing type.

Example

extension Int{
    var squareRoot : Int {
        return self * self
    }
}

let iValue = 5
print(iValue.squareRoot)

Output : 

25

Methods

Extensions add new instance method and type method to existence type.

Example

extension Int{
    func repeatValue(value : () -> Void) {
        for _ in 0..<self{
            value()
        }
    }
}

5.repeatValue {
    print("hello world")

}

Output :

hello world
hello world
hello world
hello world
hello world

For more about Extensions watch below video



Tuesday, 28 August 2018

Swift Protocol

Protocols are a way of specifying how to use of an object.

In Swift Protocols define interfaces which can be any struct, class or enum.



For use of protocol in swift watch below video





Swift MVVM design pattern


Design pattern is an important part for code reusability.

History
Since the beginning of it's development, iOS has used MVC (Model View Controller) design pattern, which helps the developers to reuse code. It also helps to keep UI and business logic separately.

Why MVVM ?
In MVC, As the View and Controller is combined in the ViewController, the controller overloaded. So in order to reduce the code size in controller, MVVM pattern has been introduced.

In MVVM pattern, ViewController does not interact directly with the Model unlike in MVC. Here, ViewController interacts with ViewModel, and ViewModel interact with the Model.

Model(Database) <-->ViewModel (Business Logic) <--> ViewController (UI/View related code)

There are some other architecture design pattern also there like MVC, MVP, MVVM, Viper etc.

Check out below link for MVVM architecture pattern example in Swift.

https://youtu.be/FBJzPppZLsg