In Swift it’s kind of unsupported to implemented optional protocol methods.
You can, which is ugly, use the @objc
syntax:
Screenshare done right—and everything else too.We can't fix Xcode's random bugs for you, but we can at least make it easier to pair in. Multi is a new video tool that lets anyone point, draw and even type in your Xcode—or any other app. When you work in Multi it automatically takes notes, including what files you looked at. Try it out for free!
@objc protocol MyProtocol {
optional func doSomething();
}
class MyClass : MyProtocol {
// no error
}
Another disadvantage here is that structs are unsupported, as you’re bridging for Objc.
Screenshare done right—and everything else too.We can't fix Xcode's random bugs for you, but we can at least make it easier to pair in. Multi is a new video tool that lets anyone point, draw and even type in your Xcode—or any other app. When you work in Multi it automatically takes notes, including what files you looked at. Try it out for free!
Using protocol extensions to create optional protocol methods
However, with Swift 2.0 it’s possible to define protocol extension. This allows you to create optional methods for your protocol easily:
protocol MyProtocol {
func doSomethingNonOptionalMethod()
func doSomethingOptionalMethod()
}
extension MyProtocol {
func doSomethingOptionalMethod(){
// leaving this empty
}
}
As stated in this thread, many people are asking this feature:
http://stackoverflow.com/questions/24032754/how-to-define-optional-methods-in-swift-protocol