Reputation: 589
I understand what purpose protocols serve (to have a type conform to a set list of methods or/and properties), but I don't understand what the purpose is of a protocol with all optional methods. One example would be UITextFieldDelegate
.
If all methods are optional in a protocol, why would you conform to the protocol instead of just writing the methods from scratch in your class? I don't see what the benefit or purpose of conforming to the protocol is in this case.
Are the optional methods there just as suggestions of functionality that could be implemented?
Upvotes: 3
Views: 439
Reputation: 437492
The protocol establishes a contract for the interface between one object and another. The fact that the methods are optional simply says that you don't have to implement that particular method, but you can if your app calls for it.
Generally, if you're conforming to a protocol for which all of the methods are optional, though, you're doing that for a reason, namely that you plan on implementing one or more of those methods. Just because all of the protocol's methods are optional doesn't mean you will not implement any of them, but rather simply that you can elect which are relevant in your particular situation.
For example, consider the UITextFieldDelegate
protocol. You'd generally conform to that because you want to specify, for example, whether certain characters should be allowed to be inserted into the text field or what to do when the return key is pressed. Sometimes you only want to implement the former. Sometimes you only want to implement the latter. Sometimes you do both. But just because you choose to implement one or the other doesn't mean you necessarily want to do other one (but you can if you want). Frankly, though, if you really didn't want to implement any of the methods, you probably wouldn't even bother to specify the delegate
of the text field, nor bother to specify that you're conforming to the protocol.
Bottom line, the protocol that consists solely of optional methods basically says "if you need it, this is the documented interface for the methods you may elect to implement". The protocol still is very useful to establish the possible interfaces, but doesn't force you to implement those methods you do not need.
Upvotes: 1
Reputation: 4268
Historically, for delegates and data sources in Cocoa, informal protocols were used. Informal protocol was implemented trough a category for NSObject
class:
@interface NSObject (NSTableViewDelegate)
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
// ...
@end
Later, optional methods in protocols were introduced. This change leads to better documenting of class responsibilities. If you see in code, that class conforms to NSTableViewDelegate
, you suspect that somewhere exists a table view, that managed by instance of this class.
Also, this change leads to stronger checks in compile time. If programmer accidentally assign wrong object to delegate
or dataSource
properties, compiler will warn.
But your assumption is also correct. Optional methods are also suggestions for possible functionality.
Upvotes: 1
Reputation: 13276
By default, all methods in a protocol are required. Each method has to be marks as optional if the nor required for everything to function correctly.
If all methods are optional in a protocol, why would you conform to the protocol instead of just writing the functions from scratch in your class?
Conforming to a protocol allow your class to tell another object the methods it has without the other object needing to know about your class. This is really useful when using Delegation as it allows the delegate to decide what information they wish to receive/provide to another class.
For example,the UIScrollViewDelegate
protocol only defines optional methods. Lets say we have a class Foo
that we want to know when things change with a UIScrollView
.
If we decided to throw that protocol away and implement the functions from scratch, how would we tell UIScrollView
which methods we implement and which methods to call when certain event occur? There is no good way it could find out. When UIScrollView
was built, it didn't know about Foo
so it can't know what methods it implements. Also, Foo
has no way of knowing what methods can be called on it by the UIScrollView
.
However, when UIScrollView
was built, it did know about UIScrollViewDelegate
. So if Foo
conforms the the UIScrollViewDelegate
protocol, there is now a common definition that both Foo
and UIScrollView
can follow. So Foo
can implement any methods it cares about, like scrollViewDidScroll:
and the UIScrollView
just needs to check if the delegate implemented the methods in UIScrollViewDelegate
.
Upvotes: 1