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?

Answers


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

Need Your Help

Calling RPC success from another RPC success

java gwt rpc

I have done a rpc in my code. On the success side, i have some code as trigger to this rpc call.

How to asynchronously download files in a specific order in iOS?

ios json multithreading asynchronous

My iPhone app makes a asynchronous request with NSURLRequest to my site.