gpt4 book ai didi

ios - 快速、objective-c 协议(protocol)实现

转载 作者:IT王子 更新时间:2023-10-29 05:40:51 26 4
gpt4 key购买 nike

还在努力适应swift,但是由于我的obj-c知识接近于0,一直没能实现这个SocketRocket协议(protocol)。任何帮助将不胜感激

这是我尝试实现的 obj-c 委托(delegate)

@protocol SRWebSocketDelegate <NSObject>

// message will either be an NSString if the server is using text
// or NSData if the server is using binary.
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;

@optional

- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;

@end

我希望这是实现它的正确方法;这不是……
我得到 'SocketDelegate' does not conform to protocol 'SRWebSocketDelegate'

class SocketDelegate:UIViewController, SRWebSocketDelegate{
let socket:SRWebSocket! = SRWebSocket()

override func loadView() {
self.socket.delegate = self
}

func didReceiveMessage(message:AnyObject){

}
}

最佳答案

答案是:

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

Functions in Swift Reference Book

Obj-C webSocket:didReceiveMessage 中的方法名是这样翻译的,比如第一部分是方法名,其他部分是外部参数名(didReceiveMessage)。另请注意,id 变为 AnyObject 并且 Obj-C 引用被翻译为 ! 为隐式解包选项(这不再是真的,隐式解包选项由于添加到 Obj-C 声明的属性,现在很少见了)。

关于ios - 快速、objective-c 协议(protocol)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25055198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com