Monday, 26 June 2017

Protocol in Swift

There are three main pillar of swift that provides facility to write your code in a way which will decrease the size of code and also improve make your code scalable , easy to read and maintainable.

These pillar are:
1) Value types
2) Functional programming
3) Protocol oriented programming

In swift for value types we use struct and enum. Value types copied when they are assigned, passed as argument to function. We won’t dive in deep for value type and functional programming here. In this blog I am going to discuss about protocol oriented programming. 


     protocol  TestProtocol{
        func  sayHello()
       
    }

    Like classes, we can inherit protocol also.
   
    struct Person: TestProtocol{
         
      //implement TestProtocol here
      func sayHello(){
       print(“Hi”)
        }

    }

   Protocol Extension:                                

 It’s not necessary to always implement a protocol . You can provide the default definition of a protocol in Protocol extension. You can also provide your implementation , in this case you will override the default implementation.

    extension TestProtocol{
    
  func sayHello(){
       print(“default implementation ”)
        }

    }

   If a protocol extension provide the definition of a function , then it’s not necessary to implement a protocol on the type which conforms it.

    struct Area:TestProtocol{

    }

    Area().sayHello


In protocol we declare the type and tell it what to do but not how to do. A protocol is like a coach , he tells the player what to do . Protocol is like interface in JAVA. Let’s understand protocol with an example:

Let’s say you have page in which you have two UILabel and a UIButton and main UIView of  your screen. Now  each UILabel and UIButton have a series of animation. 

So What approach will you take here:

1) You can directly write your animation code in main view controller in place where you want, this could be worst approach. What if you want other views to have same animation? So here comes the second approach.

2) In this approach , you can subclass your UILabel and UIButton to have animation and in future if you want other UILabel and UIButton to have same animation , you just replace their class. 

3) As you will say , swift provide even a better approach than subclassing , As we know UILabel and UIButton has the same parent UIView. So you can create an extension to UIView and write animation there. This is good approach, but this will open this for all views in your application. So here comes the Protocol .

In Protocol you just declare what to do and then each control which wants to have animation must have to implement this.

Protocol  Fadable{
}

extension Fadable where  Self:UIView{
   func fade{
      //do your animation here
    }
 }

So any UIComponents that conforms to Fadable protocol would have the fade() method with it. Here  where  Self:UIView used to indicate  that the protocol  should be only conformed  to UIView or view which are inherited from UIView.


class MyLable:UILabel, Fadable{
  //write your code
}


Reference:

2 comments:

  1. Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing. Well written article Thank You for Sharing with Us project management training in chennai | project management certification online | project management course online |

    ReplyDelete
  2. Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing.
    Be Your Own Boss! If you're looking for a change in your work prospects, then let's prepare for your career from here!!!
    Self Employment | Women Development | Information Technology | Engineering Courses


    ReplyDelete