简体   繁体   English

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

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

I am trying to use SocketRocket (an Objective-C pod) from Swift 2. I have creating a bridging header. 我正在尝试使用Swift 2中的SocketRocket(Objective-C吊舱),我已经创建了一个桥接头。

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? 我究竟做错了什么?

I figured it out. 我想到了。

didReceiveMessage method is declared required in protocol. didReceiveMessage方法在协议中声明为必需。 The problem is with your method signature. 问题出在您的方法签名上。 Your didReceiveMessage method signature does not match with the protocol's method signature. 您的didReceiveMessage方法签名与协议的方法签名不匹配。

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. 这正是Xcode抱怨缺少必需的协议方法的原因。

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. 提示:请尝试使用Xcode的智能感知来避免此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM