问题描述
我有一个自定义的ViewController委托类,它处理ui操作,例如在文本视图中单击按钮和显示文本。
I have a custom ViewController delegate class, which handles ui actions such as button clicked and display text in a text view.
现在我想在中使用AsyncSocket类。我已经能够创建一个套接字对象
Now I want to use AsyncSocket in the class. I have been able to create a socket object
var tcpSocket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
现在我想实现那些回调函数,例如didConnectToHosts(...),其声明在GCDAsyncSocket中提供.h:
Now I want to implement those callback functions, such as didConnectToHosts(...) whose declaration is provided in GCDAsyncSocket.h:
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;
如何在swift类中实现此功能?
How do I implement this function in my swift class?
推荐答案
// - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;
func socket(socket : GCDAsyncSocket, didConnectToHost host:String, port p:UInt16)
// - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
func socket(socket : GCDAsyncSocket, didReadData data:NSData, withTag tag:Int32)
不要忘记让你的swift类继承自NSObject,否则GCDAsyncSocket将无法将其指定为委托。
Don't forget to make your swift class inherit from NSObject, otherwise the GCDAsyncSocket will fail to assign it as a delegate.
另外,不要将任何参数标记为 var
,如 var didReadData数据:NSData
Also, do not mark any parameters as var
as in var didReadData data:NSData
这篇关于在swift类中实现AsyncSocket回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!