Unable to compile Swift 2 code calling into Objective C library (pod)

113 Views Asked by At

I am trying to use SocketRocket (an Objective-C pod) from Swift 2. I have creating a bridging header.

Here is what I am trying:

import SocketRocket
class WS3: NSObject, SRWebSocketDelegate {
    func websocket(webSocket: SRWebSocket!, 
                   didReceiveMessage message: AnyObject!) -> Void {
    }
}

And the compiler error message is:

Error:Error:Build failed with 1 error and 0 warnings in 1s 685ms
/Users/jao/Desktop/consulting/blackring/Black Ring/Black Ring/WS3.swift
    Error:Error:line (8)type 'WS3' does not conform to protocol 'SRWebSocketDelegate'
    x86_64
    Note:Note:class WS3: NSObject, SRWebSocketDelegate {
    Note:Note:      ^
    Note:Note:    public func webSocket(webSocket: SRWebSocket!, didReceiveMessage message: AnyObject!)
    Note:Note:                ^
SocketRocket.SRWebSocketDelegate
    Note:Note:protocol requires function 'webSocket(_:didReceiveMessage:)' with type '(SRWebSocket!, didReceiveMessage: AnyObject!) -> Void'

It looks to me like I'm doing exactly what the error message says I should be doing. What am I doing wrong?

1

There are 1 best solutions below

4
On

I figured it out.

didReceiveMessage method is declared required in protocol. The problem is with your method signature. Your didReceiveMessage method signature does not match with the protocol's method signature.

Replace it:

func websocket(webSocket: SRWebSocket!,
        didReceiveMessage message: AnyObject!) -> Void {
    }

With:

func webSocket(webSocket: SRWebSocket!,
    didReceiveMessage message: AnyObject!) {

}

This is exactly what the Xcode is complaining about that required method of protocol is missing.

I tested it at my end and it is working fine.

Tip: Please try to use Xcode's intellisense to avoid these kinds of errors.