[英]swift, objective-c protocol implementation
Still trying to get used to swift, but since my obj-c knowledge is close to 0, I have not been able to implement this SocketRocket protocol. 仍然试图习惯快速,但由于我的obj-c知识接近0,我无法实现这个SocketRocket协议。 Any help would be greatly appreciated
任何帮助将不胜感激
Here's the obj-c delegate I try to implement 这是我尝试实现的obj-c委托
@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
I hoped this was the proper way to implement it; 我希望这是实施它的正确方法; it wasn't...
它不是......
I get 'SocketDelegate' does not conform to protocol 'SRWebSocketDelegate' 我得'SocketDelegate'不符合协议'SRWebSocketDelegate'
class SocketDelegate:UIViewController, SRWebSocketDelegate{
let socket:SRWebSocket! = SRWebSocket()
override func loadView() {
self.socket.delegate = self
}
func didReceiveMessage(message:AnyObject){
}
}
The answer is: 答案是:
func webSocket(webSocket: SRWebSocket!, didReceiveMessage message: AnyObject!)
see 看到
Functions in Swift Reference Book Swift参考书中的函数
Method name in Obj-C webSocket:didReceiveMessage
is translated such as the first part is the method name, the other parts are the external parameter names ( didReceiveMessage
). Obj-C中的方法名称
webSocket:didReceiveMessage
被翻译,例如第一部分是方法名称,其他部分是外部参数名称( didReceiveMessage
)。 Also note that id
becomes AnyObject
and Obj-C references are translated with !
另请注意,
id
变为AnyObject
,Obj-C引用被翻译为!
as implicitly unwrapped optionals (this is no longer true, implicitly unwrapped optionals are now rare thanks to attributes added to Obj-C declarations). 作为隐式展开的选项(这不再是真的,由于添加到Obj-C声明的属性,隐式展开的选项现在很少见)。
另一种解决方案:尝试Starscream - 一个原生的Swift Websocket库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.