ios-swift,objective-c协议实现

前端之家收集整理的这篇文章主要介绍了ios-swift,objective-c协议实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
仍然尝试习惯,但由于我的obj-c知识接近0,所以我无法实现这个SocketRocket协议.任何帮助将不胜感激

这是我试图实现的对象代理

@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’不符合协议’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声明中的属性,隐式解开的可选项现在很少见).

原文链接:https://www.f2er.com/iOS/330168.html

猜你在找的iOS相关文章