138

I noticed methods marked optional in several protocols defined in the iPhone SDK, such as the UIActionSheetDelegate protocol for example.

How can I define a protocol of my own, and set a few of the methods as optional?

0

5 Answers 5

261

From the Apple page on "Formal Protocols":

Optional Protocol methods can be marked as optional using the @optional keyword. Corresponding to the @optional modal keyword, there is a @required keyword to formally denote the semantics of the default behavior. You can use @optional and @required to partition your protocol into sections as you see fit. If you do not specify any keyword, the default is @required.

@protocol MyProtocol

- (void)requiredMethod;

@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;

@required
- (void)anotherRequiredMethod;

@end
1
  • 4
    note that : The '''@optional''' and '''@required''' directive applies to any methods that follow it.
    – Wayne
    Apr 7, 2017 at 13:05
33

If a method in a protocol is marked as optional, you must check whether an object implements that method before attempting to call it.

As an example, the pie chart view might test for the segment title method like this:

NSString *thisSegmentTitle;
if ([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]) {
    thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];
}

The respondsToSelector: method uses a selector, which refers to the identifier for a method after compilation. You can provide the correct identifier by using the @selector() directive and specifying the name of the method.

If the data source in this example implements the method, the title is used; otherwise, the title remains nil.

3
  • 7
    Make sure to extend NSObject in your protocol to use respondsToSelector
    – Fracdroid
    Aug 28, 2015 at 22:51
  • 1
    Right, like @protocol MyProtocol <NSObject> Nov 6, 2018 at 5:41
  • This a perfect answer!
    – smoothdvd
    Dec 22, 2018 at 6:26
14

Protocols is set of rules. We can create protocols as below example:

TestProtocols.h

@protocol TestProtocols <NSObject>
    @optional
    -(void)testMethodOptional;

    @required  // by default
    -(void)testMethodRequired;
@end

Implementation:

TestClass.h

#import "TestProtocols.h"
@interface TestClass : NSObject  <TestProtocols>

@end

TestClass.m

#import "TestClass.h"
@implemenation TestClass
    //optional to implement 
    -(void)testMethodOptional{
     // Your Code
    }

    //required to implement 
    -(void)testMethodRequired{
     // Your Code
    }
@end
12

Use the @optional keyword before your method declaration to make it optional. Simple as that!

// myProtocol.h
@protocol myProtocol
- (void)myMandatoryMethod:(id)someArgument;
@optional
- (void)myOptionalMethod:(id)someArgument;
@end
// myClass.m
@interface myClass : someSuperClass <myProtocol>
    //...
@end
6

Protocols act the same as abstract classes, so the @optional keyword defines those methods that are optional for implementation.

So, in the code, someMethod1, someMethod2 and someMethod4 are required methods (must be implemented). someMethod3 is optional - if we didn't implement this method, the compiler will not throw any warnings.

@protocol myPrtocol<NSObject>

-(void)someMethod1:(id)someArgument;
-(void)someMethod2:(id)someArugument;

@optional

-(void)someMethod3:(id)someArgument;

@required //by default

-(void)someMethod4:(id)someArgument;

@end

// sampleClass.m
@interface sampleClass : someSuperClass <myProtocol>
//...
@end

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.