Look at improved way of optional methods in swift using an extension (below)
– Daniel KanaanNov 17 '15 at 19:05

1

And how does one test for support of an optional protocol method in an instance? respondsToSelector?
– devios1Jan 27 '16 at 21:35

3

Just don't do this! Swift does not support optional method in protocols for a reason.
– fpg1503Jun 22 '16 at 19:09

2

This method doesn't support optionals in parameters. I.e. you can't do that optional func doSomething(param: Int?)
– SoftDesignerNov 12 '16 at 10:20

3

Surely this answer is essentially wrong nowadays, years later. Today in Swift you simply add an extension for the default implementation, it's a basic aspect of Swift. (As all the modern answers below show.) It would be just wrong to add the objc flag, nowadays.
– FattieJun 26 '17 at 10:54

This is probably the cleanest way to do it in Swift. Too bad it does not work prior to Swift 2.0.
– EntalpiSep 3 '15 at 8:32

11

@MattQuiros I'm finding that you do in fact need to declare the function in the protocol definition, otherwise the no-op extension function doesn't get overridden in your classes that conform to the protocol.
– Ian PearceSep 30 '15 at 18:13

3

@IanPearce is correct, and this appears to be by design. In the "Protocol Oriented Programming" talk (408) at WWDC, they talk about methods in the main protocol being "customization points" offered to conforming types. A required point of customization doesn't receive a definition in an extension; an optional point does. Methods on the protocol that generally should not be customized are wholly declared/defined in the extension to disallow conforming types to customize unless you specifically cast down to its dynamicType to show you want the conformer's custom implementation.
– matthiasOct 1 '15 at 9:54

@FranklinYu You can do this but then you are poluting your API design with 'throws' where it's in fact not needed. I like more the idea of "micro protocols". E.g. each method confirms to one protocol and then you can check for: if object is protocol
– DarkoJun 9 '16 at 21:28

Since there are some answers about how to use optional modifier and @objc attribute to define optional requirement protocol, I will give a sample about how to use protocol extensions define optional protocol.

I think that before asking how you can implement an optional protocol method, you should be asking why you should implement one.

If we think of swift protocols as an Interface in classic object oriented programming, optional methods do not make much sense, and perhaps a better solution would be to create default implementation, or separate the protocol into a set of protocols (perhaps with some inheritance relations between them) to represent the possible combination of methods in the protocol.

Here's a very simple example for swift Classes ONLY, and not for structures or enumerations.
Note that the protocol method being optional, has two levels of optional chaining at play.
Also the class adopting the protocol needs the @objc attribute in its declaration.

Can you describe a little more the "two levels of optional at play" part, namely this: delegate?.indexDidChange?(index!)?
– UnheiligMar 5 '15 at 20:40

2

If we had written the protocol to have a non optional method like this: protocol CollectionOfDataDelegate{ func indexDidChange(index: Int) } then you would call it without the question mark: delegate?.indexDidChange(index!) When you set a optional requirement for a method in a protocol, the Type that will conform to it might NOT implement that method, so the ? is used to check for the implementation and If there is none the program will not crash. @Unheilig
– Blessing LopesMar 6 '15 at 18:43

@BlessingLopes Can you add your explanation of the delegate? usage to your answer? That information should really belong in there for others in the future. I want to upvote this, but that information should really be in the answer.
– Johnathon SullingerJul 6 '15 at 19:29

To define OptionalProtocol 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.