Swift Protocol Optional Methods

By default swift don’t provide mechanism for optional methods as in Objective C. But same can be achieved by using following ways:

  1. By marking @Objc on before protocol name as given below:

This way use Objective C library to create optional method. It is not a best practice to use optional method as this code won’t be compilable of pure swift platform as it uses Objective C method.

2. By using extension in swift:

You can see that we just provide empty body for method to which we want to make optional. This is best practice as it uses native Swift code to implement optional methods.