0
votes

I am trying to extend an ObjC class for dependency injection using a protocol in swift , this class isn't owned by me so I cant make changes. For one of the method its working but for other Xcode always says that the class isn't confirming to the protocol and suggests me to add the method to the extension

Below is the declaration in ObjC header

- (BOOL) subscribeToTopic:(NSString *)topic
                      QoS:(AWSIoTMQTTQoS)qos
          extendedCallback:(AWSIoTMQTTExtendedNewMessageBlock)callback;

Here is the protocol and its extension

protocol PopAWSIoTDataManagerProtocol {

    func publishString(_ data:String, onTopic:String, QoS:AWSIoTMQTTQoS) -> Bool
    func subscribeToTopic(_ topic: String, qoS: AWSIoTMQTTQoS, extendedCallback: (NSObject, String, Data) -> Void) -> Bool

}

extension AWSIoTDataManager : PopAWSIoTDataManagerProtocol {

}

Notice the error below it suggests me exactly same func to be added to extension which I already have added to main protocol

enter image description here

Not sure whats wrong , as I was able to add another method just fine.

Article used as a reference is this https://medium.com/flawless-app-stories/the-complete-guide-to-network-unit-testing-in-swift-db8b3ee2c327

2
If I just keep the publishString method all runs finevishal dharankar

2 Answers

0
votes

You must confirm protocol PopAWSIoTDataManagerProtocol, cuz it extension of class AWSIoTDataManager

Or you can try this

extension PopAWSIoTDataManagerProtocol where Self: AWSIoTDataManager {
  ...
}
0
votes

The protocol that you have defined has all function's to implemented as mandatory.

You need to define them as optional. Either you define them as optional or implement all of them which was the error image that you have attached is asking for.

@objc protocol PopAWSIoTDataManagerProtocol {

    @objc optional func publishString(_ data:String, onTopic:String, QoS:AWSIoTMQTTQoS) -> Bool
    @objc optional func subscribeToTopic(_ topic: String, qoS: AWSIoTMQTTQoS, extendedCallback: (NSObject, String, Data) -> Void) -> Bool

}

Default behavior of Protocol, when implemented is that you have to override all the methods. These are categorized as Protocol Requirements.

There are Optional Protocol Requirements , which do not have to be implemented by types that conform to the protocol.

To achieve Optional Protcol Requirements, the protocol and the requirement i.e the optional function must be marked as @objc.