[英]"Type of expression is ambiguous" error about UDP socket with GCD in Swift (DispatchQueue)
When I coding TCP...当我编码 TCP 时...
socket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
That's good那挺好的
But when I coding UDP...但是当我编码 UDP 时...
udpSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: DispatchQueue.main)
the error says Type of expression is ambiguous with more context
错误说Type of expression is ambiguous with more context
I don't know how can I fix it.我不知道我该如何修复它。 I searched the definition of "DispatchQueue" and GCD but I still have no answer on it.我搜索了“DispatchQueue”和 GCD 的定义,但仍然没有答案。
If anyone can help me, I would be very grateful !!!如果有人可以帮助我,我将不胜感激!!!
override func viewDidLoad(){
super.viewDidLoad()
udpSocket = GCDAsyncUdpSocket( delegate: self, delegateQueue: DispatchQueue.main)
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?){
print("didReceiveData")
var host: NSString?
var port: UInt16 = 0
GCDAsyncUdpSocket.getHost(&host, port: &port, fromAddress: address)
showMessage("From IP: \(String(data: data, encoding: String.Encoding.utf8)!)")
print("From \(host!)")
print("incoming message: \(String(data: data, encoding: String.Encoding.utf8)!)")
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?){
print("didNotConnect \(error!)")
}
func udpSocketDidClose(_ sock: GCDAsyncUdpSocket, withError error: Error?){
print("斷開連線error: \(error!)")
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didNotSendDataWithTag tag: Int, dueToError error: Error?){
print("didNotSendDataWithTag")
}
func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int){
print("didSendDataWithTag")
}
由于您使用self
作为委托,请确保您的类为委托实现了协议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.