Home > DeveloperSection > Forums > Creating our own optional methods in protocol
Karen Green
Karen Green

Total Post:6

Posted on    October-30-2015 5:42 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 404  View(s)
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?

Tarun Kumar

Total Post:210

Posted on    October-30-2015 6:20 AM

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;

  - (void)optionalMethod1;
  - (void)optionalMethod2;

  - (void)requiredMethod;



Don't want to miss updates? Please click the below button!

Follow MindStick