Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Protocols it's usage and Types

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 460
    Comment on it

    Protocols are powerful functionality provided by iOS. These are set of behavior that is expected from an object to behave/respond in a given situation. Protocols are set of methods and properties. One can define protocol as:

    @protocol protocolName

    // methods and properties

    @end

    Types of Protocols

    There are two kinds of protocols

    1- Required: These are mandatory and should be used when a class is adopting a protocol. By default all protocols are required one can use @required to make methods required/mandatory

    2- Optional: These are not mandatory, If a class implements protocol then optional protocols are not mandatory to implement. One can make methods optional using @optional.

    We must check before calling optional protocols as below:

    if([self.delegate respondsToSelector:@selector(showTheMax:)])
    {
    
    // This means class is implementing the optional protocol method.
    // code here.
    }
    

    A class can adopt a protocol using angle<> brackets. Here is the syntax how one should conform to a protocol:
    @interface ABClass: NSObject

    This means that any instance of ABClass will respond not only to the methods declared specifically in the interface, but ABClass also provides implementations for the required methods in MyDefinedProtocol. One need not to to re-declare the protocol methods in the ABClass class interface because the adoption of the MYDefineProtocol is sufficient.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: