Swift does not allow protocols to have optional requirements - if the protocol declares something, it is necessary. Objective-C has long been aware of additional requirements, and Swift recognizes this when you use @objc when declaring a protocol. So using @objc is an easy way to get what you need.
If you want a clean Swift solution, you need to add a protocol extension that includes standard method implementations. This does not make these methods optional; instead, he says that any class that does not implement the method itself will use the default implementation instead. They are optional in these classes, they do not need to implement them, but not very optionally, because this happens only because the default implementation is available.
It will look something like this:
protocol DrawViewProtocol : class{ func drawViewDidEndEditing() } extension DrawViewProtocol { func drawViewDidEndEditing() {} } class MyClass : DrawViewProtocol { } class MyOtherClass : DrawViewProtocol { func drawViewDidEndEditing() { print("Foo") } }
Now, if I create an instance of MyClass and call drawViewDidEndEditing() , it uses the default implementation, which does nothing. If I create an instance of MyOtherClass , the same method will print "Foo".
source share