iosobjective-cswift3swift-protocolssocketrocket

Instance method nearly matches optional Requirement


I have a class in swift which implements SRWebSocketDelegate protocol written in objective-c. When I try to implement it's optional method webSocketDidOpen, then I get the following warning: enter image description here

Also, when I run my code the implementation for webSocketDidOpen is not getting called and the code directly goes to webSocket(_ webSocket: SRWebSocket, didReceiveMessageWith string: String)

How do I implement the optional methods of my protocol written in Objective-C and also get rid of these warnings?

Thanks in advance!


Solution

  • SRWebSocket.h is annotated NS_ASSUME_NONNULL_BEGIN, so by default all values are nonnull. That means that this:

    - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
    

    Is imported as:

    func webSocketDidOpen(_ webSocket: SRWebSocket)
    

    But you implemented:

    func webSocketDidOpen(_ webSocket: SRWebSocket!)
    

    SRWebSocket is not the same type as SRWebSocket!. Remove the !. This is likely what the rename fixit will do for you.