CREATING OUR OWN OPTIONAL METHODS IN PROTOCOL

Karen Green

Total Post:6

Points:42
Posted by  Karen Green
 704  View(s)
Ratings:
Rate this:
I have notice that optional methods in several protocols are defined in the iPhone SDK, such as the UIActionSheetDelegate protocol.
So my problem is that can I define a protocol of my own, and set a few of the methods as optional?
  1. Tarun Kumar

    Post:214

    Points:1642
    Re: Creating our own optional methods in protocol

    When we create a Protocol then we need to mark '@optional' keyword, because it is responsible to create our own optional method.
    Corresponding to the '@optional' keyword, here is another keyword @required, used when we want any method necessary in our code, using @optional and @required keywords we can do partition in protocol into sections and by the way by default keyword is @required in any protocol.

    @protocol ProtocolSample

    - (void)defaultRequiredMethod;

    @optional
      - (void)optionalMethod1;
      - (void)optionalMethod2;

    @required
      - (void)requiredMethod;

    @end

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!