Swift 3: Protocol Part 2


So moving onward from part 1 to explore more about the Protocol. Let's start it!! below mentioned example checked on Xcode 8.1

Protocol Property Requirements - 


the protocol requires any conforming type to provide instance and type property with name and type. We can mention property as getter or setter or both types (Ex-1). Property needs to implement as per type to satisfies Protocol requirements.

         Property requirement always needs to declare as var. Property can be declared as gettable and settable, {get}  and {get set} respectively.
    (Ex-1)

    protocol myProtocol {

        var canBeSetAndGet: Int { get set }

        var canBeGet: Int { get }

    }


    Type property can be prefixed with a class or static keyword when you declare in Protocol.
         

    protocol myProtocol {

        static var myProperty:String {get set}

    }


          

    protocol myProtocol {

        var myProperty:String {get set}

    }


    Protocol Method Requirements - 

    The protocol requires all instance and type methods to implemented by adopting types. We can declare the simply without adding curly braces in a protocol.

    protocol myProtocol {

        static func someMethod()

    }


    Type method requirements for the protocol are the same way as type property for the protocol. We need to add the same static or class keyword prefix for the type method when implemented by the type.

                 By default, all properties and methods would be required. An optional protocol has to be objective c compatible and can be adopted by class only. As per the latest @objc, we need to use before protocol and optional method.

    @objc protocol SomeProtocol {

        @objc optional static func test()

      }


    The @Objc keyword is for Objective - c interoperability and it indicates that your class or protocol should be accessible to Objective - C code.


    Mutating Protocol Method Requirements - 

    Sometimes we need to modify the instance which belongs to the function then we put the mutating keyword before the function to mutate the instance and instance properties.
         
    As Apple said:
    If you mark a protocol instance method requirement as mutating, you do not need to write the mutating keyword when writing an implementation of that method for a class. The mutating keyword is only used by structures and enumerations.

      protocol Togglable {

           mutating func toggle()

      }



      enum StartStopWatch: Togglable {

          case stop, start

          mutating func toggle() {

              switch self {

              case .stop:

                  self = .start // mutating instance

              case .start:

                  self = .stop // mutating instance

              }

          }

      }

      var lightSwitch = StartStopWatch.start

      lightSwitch.toggle() // off



      Protocol Initializers  - 
           We can write a declaration of initializers in the protocol definition. This will be implemented by the conforming types. While giving implementation use the required keyword. Which ensures that given initializers requirement or inherited the implementation of all conforming class.

              The class which is marked with the final can not use the required because final classes cannot be subclassed.

      NoteIf the subclass overrides the designated initializer from its superclass and the same method from protocol the mark that initializer method with required override.


       protocol myProtocol {

         init()

      }


      class mySuperClass {

          init() {}

      }


       class mySubClass: mySuperClass, myProtocol {

          required override init() { }

      }



        Failable Initializers Requirements :

         A failable initializer requirement can be full fill by failable and non-failable initializer by conforming types and non-failable requirement can be fully filled by the non-failable initializer  
          

              Protocol conformance with Extension :

         We can extend the functionality of extension by conforming to a new protocol.
                 

        class mySuperClass {

            init() {}

        }


        protocol TexToRepresent {

            var textRepresntataion: String { get }

        }

          

        extension mySuperClass: TexToRepresent {

              var textRepresntataion: String {

                  return "text representation"

              }

        }


        var myobj = mySuperClass()

        myobj.textRepresntataion //"text representation"



         If a type already conforms to all requirements of the protocol and did not adopt that protocol then we adopt that protocol with an empty extension of that type.

        Wrapping UP :

         That's all from Part -2. I will be coming soon with the last Part -3 of Protocol. Guys, you have any clarification or confusion. Please, do comment. 



        Comments

        Popular posts from this blog

        Copy Vs Mutable Copy in iOS

        How to Disable iOS/iPad App availability on Mac Store, Silicon?

        Closures in Swift - Part 1