3

Using @objc in swift, we can create an optional methods inside protocol like

@objc protocol MyProtocol {
  @objc optional func anOptionalMethod()
}

But how to create an optional protocol methods without using @objc ?

Ashis Laha
  • 2,176
  • 3
  • 16
  • 17
  • 2
    You cannot, because they are specific to ObjC. The easiest way is to simply provide empty default implementations in an extension and then override those as needed. – BallpointBen Feb 17 '18 at 04:55

4 Answers4

22

You can define default func implementation by:

protocol Opt {
    func requiredFunc()
    func optionalFunc()
}

extension Opt {
    func optionalFunc() {}
}

With this you don't have to implement optionalFunc() in classes conforming to Opt, because they already have their default implementation.

tereks
  • 1,256
  • 9
  • 15
1

Let's create an optional protocol methods using extension of that protocol and use it:

   //: Playground - noun: a place where people can play

   import UIKit

   protocol someProtocol {
     func a()
     func b()
   }

  extension someProtocol {
    func c() {
        print("This is optional protocol method")
     }

    var someProperty: String { 
        return "some optional computed property"
    }
  }

 class A: someProtocol {
   func a() {
       print("a")
    }

   func b() {
      print("b")
   }

   // It's upto you to implement c() functionality, 
   // this is an optional protocol method

   func c() {
      print("I am overriding C functionality")
   }
  }

 let obj = A()
 obj.a()
 obj.b()
 obj.c()
 print(obj.someProperty)

Output:

 a
 b
 I am overriding C functionality
 some optional computed property
Ashis Laha
  • 2,176
  • 3
  • 16
  • 17
  • 1
    This is not an optional protocol method. Whether or not it's overriden, there's always an implementation done, so it's not optional. – Jake Feb 17 '18 at 05:17
1

Swift protocols do not support optional methods.

Apple Docs:

Optional Protocol Requirements

You can define optional requirements for protocols, These requirements don’t have to be implemented by types that conform to the protocol. Optional requirements are prefixed by the optional modifier as part of the protocol’s definition. Optional requirements are available so that you can write code that interoperates with Objective-C. Both the protocol and the optional requirement must be marked with the @objc attribute. Note that @objc protocols can be adopted only by classes that inherit from Objective-C classes or other @objc classes. They can’t be adopted by structures or enumerations.

Apple Protocol Documentation

Jake
  • 13,097
  • 9
  • 44
  • 73
  • Apple uses the keyword "optional" in many of their protocols, written in Swift, without using @objc. (Example: UITextFieldDelegate) Any idea how? – Marcy Mar 05 '20 at 19:33
  • I believe UITextFieldDelegate is actually defined as an ObjC interface – Jake Mar 05 '20 at 19:36
  • I think you're probably right. I'm just not seeing @interface or Objective-C code but rather Swift and the protocol keyword. So, I'm a bit confused as to the mechanism that's making that work. Thank-you for your response. – Marcy Mar 06 '20 at 00:35
1

One common use for optional methods in protocols is to define optional callbacks a delegate can listen for. This scenario is best achieved using the protocol + extension approach (to provide do-nothing default implementations for tall of the optional methods).

For some other cases, it may be more appropriate to use multiple protocols:

protocol Storage {
    func read() -> Data
}

protocol MutableStorage: Storage {
    func write(data: Data
}

The standard library uses this approach for Sequence, Collection, and friends.

Alexander
  • 59,041
  • 12
  • 98
  • 151