无法编译Swift 2代码调用Objective C库(pod)

时间:2016-02-15 23:08:59

标签: objective-c swift2 socketrocket

我正在尝试使用Swift 2中的SocketRocket(Objective-C pod)。我创建了一个桥接头。

以下是我的尝试:

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

编译器错误消息是:

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'

在我看来,我正在做出错误信息所说的我应该做的事情。我做错了什么?

1 个答案:

答案 0 :(得分:1)

我明白了。

协议中声明了

didReceiveMessage方法。问题出在您的方法签名上。您的didReceiveMessage方法签名与协议的方法签名不匹配。

替换它:

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

使用:

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

}

这正是Xcode抱怨所需的协议方法缺失的原因。

我在最后测试了它并且工作正常。

提示:请尝试使用Xcode的intellisense来避免这些错误。