There is @optional keyword for protocol in Objective-C, which indicates you can ignore these methods when conforming this protocol. It is quite common for us to define a set of methods in a protocol, and the delegate class only implements several of them. In Cocoa API and Objective-C, there are a number of examples of optional methods in protocol. UITableViewDataSource and UITableViewDelegate are both good examples of it. There are only two required methods in the former:


They are necessary to calculate the height of a table view and what the cell looks like. Without these information, UIKit will not know how to show the tableView. However, other methods, such as asking for a count of sections or whether a cell can be edited or not, are all optional, since there are default behaviours for them. And all methods in the latter one (UITableViewDelegate) are optional. They are used for detail configuration and events of the table view.

But it turns to be quite different when it comes to Swift. All methods and properties MUST be implemented in native Swift. If you are trying to add something like optional before a pure Swift protocol, it won't compile at all.

If you really want to add optional methods in Swift protocol, the only way would be declaring it as an Objective-C protocol. You can add @objc keyword before the definition of protocol. Then you can use optional to define an optional method (Please note the modifier is different from @optional in Objective-C. There is no @ sign in Swift optional):

@objc protocol OptionalProtocol {
    optional func optionalMethod()

For all the methods, the scope of modifiers is separated, which is also different from Objective-C. A method without modifier means a required one:

@objc protocol OptionalProtocol {
    optional func optionalMethod()         // Optional
    func necessaryMethod()                 // Required
    optional func anotherOptionalMethod()  // Optional

There is an inevitable limit when using @objc in a Swift protocol. It will make this protocol can only be conformed by a class. For struct and enum, we cannot make them conform a protocol with optional methods or properties.