jpm
jpm

Reputation: 16702

How to create a protocol with methods that are optional?

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?

Upvotes: 139

Views: 88516

Answers (5)

user3540599
user3540599

Reputation: 721

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

Upvotes: 6

Vikram Biwal
Vikram Biwal

Reputation: 2826

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

Upvotes: 14

Zephyr
Zephyr

Reputation: 6351

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.

Upvotes: 34

Matt Gallagher
Matt Gallagher

Reputation: 14968

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

Upvotes: 262

James Eichele
James Eichele

Reputation: 119144

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

Upvotes: 12

Related Questions