Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of an optional method in a protocol?

If I'm going to have to call "respondsToSelector:" on the object anyway, what does defining a method as optional actually do for me?

For exmple, let's say I have some code like this

id<MyProtocol> myObj = [[MyClass alloc] init];
if([myObj respondsToSelector:@selector(aMethod)]){
  [myObject aMethod];
}

As long as "MyClass" implements "aMethod", won't this code run exactly the same wether or not MyProtocol defines "aMethod"?

I can see the use of defining this optional protocol purely from a code readability standpoint, but don't understand if it actually has any effect from a technical standpoint (other than making it unnecessary to declare the method in the header).

like image 605
morgancodes Avatar asked Jan 29 '11 20:01

morgancodes


People also ask

What is optional method in protocol in Swift?

Swift protocols on their side do not allow optional methods. But if you are making an app for macOS, iOS, tvOS or watchOS you can add the @objc keyword at the beginning of the implementation of your protocol and add @objc follow by optional keyword before each methods you want to be optional.

How do you make a function optional in protocol?

To define Optional Protocol in swift you should use @objc keyword before Protocol declaration and attribute / method declaration inside that protocol. Below is a sample of Optional Property of a protocol.


2 Answers

Pretty much what it says on the tin: it relates to optional functionality. If your aMethod contains essential functionality in order for your application to work it should be @required. Otherwise if it provides additional functionality for the implementer to do other things, but its absence won't negatively affect the way it should work (i.e. it's alright that the implementer doesn't respond to the @selector(aMethod) selector), you can make it @optional.

You see this a lot in view delegate protocols. Take iOS's UITableViewDelegate for example: here is a set of the delegate protocol's methods that define views for a table view's section headers and footers:

  • tableView:viewForHeaderInSection:
  • tableView:viewForFooterInSection:
  • tableView:heightForHeaderInSection:
  • tableView:heightForFooterInSection:

If these are not implemented by the delegate, UIKit simply draws the default section headers and footers for the given tableView, that come pre-packaged with the default UITableView element. However, if they are implemented, UIKit uses the custom section header and footer views that are supplied by these methods with the tableView.

The @optional keyword pretty much tells the person writing a class to implement the delegate that these methods are optional. I believe UIKit does conformsToProtocol: and respondsToSelector: checks internally anyway.

like image 77
BoltClock Avatar answered Oct 09 '22 10:10

BoltClock


The @optional/@required are not just for people - though that would be a good reason itself! - it is also for machines. The compiler, static analyzer, etc. can use them to determine whether a class implementing a @protocol has provided all the required methods, and determine which optional methods are provided.

like image 20
CRD Avatar answered Oct 09 '22 09:10

CRD